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.specloader.collectiontyperegistry;
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.List;
025import java.util.Set;
026
027import org.apache.isis.core.metamodel.spec.feature.OneToManyFeature.CollectionSemantics;
028
029public class CollectionTypeRegistryDefault extends CollectionTypeRegistryAbstract {
030
031    private final List<Class<?>> collectionTypes = new ArrayList<Class<?>>();
032    private Class<?>[] collectionTypesAsArray = new Class[0];
033
034    /**
035     * Inbuilt support for {@link Collection} as a collection type.
036     * 
037     * <p>
038     * Note that this includes any subclasses.
039     */
040    public CollectionTypeRegistryDefault() {
041        addCollectionType(Collection.class);
042    }
043
044    /**
045     * Plan is for this to be promoted to API at some stage.
046     */
047    private void addCollectionType(final Class<?> collectionType) {
048        collectionTypes.add(collectionType);
049        collectionTypesAsArray = collectionTypes.toArray(new Class[0]);
050    }
051
052    @Override
053    public boolean isCollectionType(final Class<?> cls) {
054        return java.util.Collection.class.isAssignableFrom(cls);
055    }
056
057    @Override
058    public boolean isArrayType(final Class<?> cls) {
059        return cls.isArray();
060    }
061
062    @Override
063    public Class<?>[] getCollectionType() {
064        return collectionTypesAsArray;
065    }
066
067    @Override
068    public CollectionSemantics semanticsOf(final Class<?> underlyingClass) {
069        if (!Collection.class.isAssignableFrom(underlyingClass)) {
070            return CollectionSemantics.ARRAY;
071        }
072        if (List.class.isAssignableFrom(underlyingClass)) {
073            return CollectionSemantics.LIST;
074        }
075        if (Set.class.isAssignableFrom(underlyingClass)) {
076            return CollectionSemantics.SET;
077        }
078        return CollectionSemantics.OTHER;
079    }
080
081}