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

org.nakedobjects.metamodel.util.CollectionFacetUtils Maven / Gradle / Ivy

The newest version!
package org.nakedobjects.metamodel.util;

import java.util.ArrayList;
import java.util.List;

import org.nakedobjects.metamodel.adapter.NakedObject;
import org.nakedobjects.metamodel.facets.collections.modify.CollectionFacet;
import org.nakedobjects.metamodel.spec.NakedObjectSpecification;


public final class CollectionFacetUtils {
    private CollectionFacetUtils() {}

    public static CollectionFacet getCollectionFacetFromSpec(final NakedObject objectRepresentingCollection) {
        final NakedObjectSpecification collectionSpec = objectRepresentingCollection.getSpecification();
        return collectionSpec.getFacet(CollectionFacet.class);
    }

    public static int size(final NakedObject collection) {
        final CollectionFacet facet = getCollectionFacetFromSpec(collection);
        return facet.size(collection);
    }

    public static NakedObject firstElement(final NakedObject collection) {
        final CollectionFacet facet = getCollectionFacetFromSpec(collection);
        return facet.firstElement(collection);
    }

    /**
     * @deprecated - use instead {@link #convertToList(NakedObject)}.
     */
    @Deprecated
    public static Object[] convertToArray(final NakedObject collection) {
    	return convertToList(collection).toArray();
    }

    public static List convertToList(final NakedObject collection) {
        final CollectionFacet facet = getCollectionFacetFromSpec(collection);
        final List objects = new ArrayList();
        for(NakedObject adapter: facet.iterable(collection)) {
        	objects.add(adapter.getObject());
        }
        return objects;
    }

    public static List convertToAdapterList(final NakedObject collection) {
        final CollectionFacet facet = getCollectionFacetFromSpec(collection);
        final List adapters = new ArrayList();
        for(NakedObject adapter: facet.iterable(collection)) {
        	adapters.add(adapter);
        }
        return adapters;
    }

}