Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
OpenBlueSky - NetBeans Platform Enhancements Copyright (C) 2006-2011 by Tidalwave s.a.s. (http://www.tidalwave.it) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. WWW: http://openbluesky.kenai.com SCM: https://kenai.com/hg/openbluesky~src /
 
 package it.tidalwave.hierarchy.spi.simple;
 
 import  javax.annotation.Nonnull;
 import java.util.List;
 import java.util.Map;
 import  org.openide.util.Lookup;
 import  it.tidalwave.util.Id;
 import  it.tidalwave.role.Identifiable;
 import static it.tidalwave.role.Displayable.Displayable;

Author(s):
Fabrizio Giudici
Version:
$Id$ /
 
 public abstract class ParentSupport<T extends Identifiable> extends Support
   {
     private final List<T> items = new ArrayList<T>();
     private final Map<Id, T> itemMapById = new HashMap<Id, T>();
 
     public ParentSupport (final @Nonnull SimpleBuilder<?> builder,
                           final @Nonnull String toString,
                           final @Nonnull Lookup.Provider parent,
                           final @Nonnull Object ... capabilities)
       {
         super(buildertoStringparentcapabilities);
       }
 
     @Nonnull
     public Finder<T> findChildren()
       {
         return new HFinderSupport<T>("findChildren()")
           {
             @Override @Nonnull
             protected List<T> computeResults()
               {
                 if ( != null)
                   {
                     final T view = .get();
                     return (view != null) ? Collections.<T>singletonList(view) : Collections.<T>emptyList();
                   }
                 else
                   {
                     return new ArrayList<T>();
                   }
               }
           };
       }
 
     @Nonnull
     public Builder<T> createChild()
       {
         return new SimpleBuilder<T>()
           {
             @Override @Nonnull
             public T build()
               {
                 return doCreateItem(this);
               }
           };
       }
 
     @Nonnull
     public Builder<T> findOrCreateChild()
       {
         return new SimpleBuilder<T>()
           {
             @Override @Nonnull
             public T build()
               {
                 if (.containsKey())
                  {
                    return .get();
                  }
                return doCreateItem(this);
              }
          };
      }
    @Nonnull
    private T doCreateItem (final @Nonnull SimpleBuilder<T> builder)
      {
        final T item = createItem(builder);
        .add(item);
        .put(item.getId(), item);
        return item;
      }
    @Nonnull
    protected abstract T createItem (final @Nonnull SimpleBuilder<T> builder);
    @Override @Nonnull
    public String toString()
      {
        String name = "";
        try
          {
            name = as(Displayable).getDisplayName();
          }
        catch (Exception e// some implementations could not be always available (e.g. transactional)
          {
            name = "<n.a>";
          }
        return String.format("%s@%x[%s, %s]"getClass().getSimpleName(),
                                          System.identityHashCode(this),
                                          name);
      }
  }
New to GrepCode? Check out our FAQ X