All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.indexeddataobject.IndexedDataObjectList Maven / Gradle / Ivy

There is a newer version: 1.5.20
Show newest version
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2012 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.java.net
 * SCM: https://bitbucket.org/tidalwave/openbluesky-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.indexeddataobject;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.io.IOException;
import org.openide.loaders.DataObject;
import it.tidalwave.util.Id;
import it.tidalwave.role.Identifiable;
import it.tidalwave.netbeans.util.Locator;

/***********************************************************************************************************************
 *
 * This special implementation of {@link List} holds a collection of {@link IndexedDataObject} without keeping direct 
 * references to them; instead their ids are stored.
 *
 * TODO: check if you can rather held the serialized bytes; this would work the same for indexed DataObjects, but you
 * could drop the dependency on IndexedDataObjectManager and move to OpenBlueSky - EnhancedDataObject.
 * EVEN BETTER, you could define an injectable Handle role and use it!
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class IndexedDataObjectList extends AbstractList
  {
    private final IndexedDataObjectManager dataObjectManager = Locator.find(IndexedDataObjectManager.class);
    
    private final List ids = new ArrayList();

    /***************************************************************************
     * 
     *
     **************************************************************************/
    public IndexedDataObjectList()
      {
      }
    
    /***************************************************************************
     * 
     *
     **************************************************************************/
    public IndexedDataObjectList (final Collection collection)
      {
        addAll(collection);
      }
    
    /***************************************************************************
     * 
     * {@inheritDoc}
     *
     **************************************************************************/
    @Override
    public void add (final int index, final T element) 
      {
        ids.add(index, element.getLookup().lookup(Identifiable.class).getId()); 
      }

    /***************************************************************************
     * 
     * {@inheritDoc}
     *
     **************************************************************************/
    @Override
    public T remove (int index) 
      {
        final T result = get(index);
        ids.remove(index);
        return result;
      }

    /***************************************************************************
     * 
     * {@inheritDoc}
     *
     **************************************************************************/
    @Override
    public T get (int index) 
      {
        try 
          {
            return (T)dataObjectManager.findById(ids.get(index));
          } 
        catch (IOException e) 
          {
            throw new RuntimeException(e);
          }
      }   

    /***************************************************************************
     * 
     * {@inheritDoc}
     *
     **************************************************************************/
    @Override
    public int size() 
      {
        return ids.size();
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy