001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.metamodel.facets.collections.modify; 021 022import java.util.ArrayList; 023import java.util.List; 024 025import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 026import org.apache.isis.core.metamodel.spec.ObjectSpecification; 027 028public final class CollectionFacetUtils { 029 private CollectionFacetUtils() { 030 } 031 032 public static CollectionFacet getCollectionFacetFromSpec(final ObjectAdapter objectRepresentingCollection) { 033 final ObjectSpecification collectionSpec = objectRepresentingCollection.getSpecification(); 034 return collectionSpec.getFacet(CollectionFacet.class); 035 } 036 037 public static int size(final ObjectAdapter collection) { 038 final CollectionFacet facet = getCollectionFacetFromSpec(collection); 039 return facet.size(collection); 040 } 041 042 public static ObjectAdapter firstElement(final ObjectAdapter collection) { 043 final CollectionFacet facet = getCollectionFacetFromSpec(collection); 044 return facet.firstElement(collection); 045 } 046 047 /** 048 * @deprecated - use instead {@link #convertToList(ObjectAdapter)}. 049 */ 050 @Deprecated 051 public static Object[] convertToArray(final ObjectAdapter collection) { 052 return convertToList(collection).toArray(); 053 } 054 055 public static List<Object> convertToList(final ObjectAdapter collection) { 056 final CollectionFacet facet = getCollectionFacetFromSpec(collection); 057 final List<Object> objects = new ArrayList<Object>(); 058 for (final ObjectAdapter adapter : facet.iterable(collection)) { 059 objects.add(adapter.getObject()); 060 } 061 return objects; 062 } 063 064 public static List<ObjectAdapter> convertToAdapterList(final ObjectAdapter collection) { 065 final CollectionFacet facet = getCollectionFacetFromSpec(collection); 066 final List<ObjectAdapter> adapters = new ArrayList<ObjectAdapter>(); 067 for (final ObjectAdapter adapter : facet.iterable(collection)) { 068 adapters.add(adapter); 069 } 070 return adapters; 071 } 072 073}