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
020 package org.apache.isis.core.progmodel.facets.collections.collection;
021
022 import java.util.ArrayList;
023 import java.util.Collection;
024
025 import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
026 import org.apache.isis.core.metamodel.adapter.map.AdapterMap;
027 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
028 import org.apache.isis.core.metamodel.facets.collections.CollectionFacetAbstract;
029
030 public class JavaArrayFacet extends CollectionFacetAbstract {
031
032 private final AdapterMap adapterMap;
033
034 public JavaArrayFacet(final FacetHolder holder, final AdapterMap adapterManager) {
035 super(holder);
036 this.adapterMap = adapterManager;
037 }
038
039 /**
040 * Expected to be called with a {@link ObjectAdapter} wrapping an array.
041 */
042 @Override
043 public void init(final ObjectAdapter collectionAdapter, final ObjectAdapter[] initData) {
044 final int length = initData.length;
045 final Object[] array = new Object[length];
046 for (int i = 0; i < length; i++) {
047 array[i] = initData[i].getObject();
048 }
049 collectionAdapter.replacePojo(array);
050 }
051
052 /**
053 * Expected to be called with a {@link ObjectAdapter} wrapping an array.
054 */
055 @Override
056 public Collection<ObjectAdapter> collection(final ObjectAdapter collectionAdapter) {
057 final Object[] array = array(collectionAdapter);
058 final ArrayList<ObjectAdapter> objectCollection = new ArrayList<ObjectAdapter>(array.length);
059 for (final Object element2 : array) {
060 final ObjectAdapter element = getAdapterMap().getAdapterFor(element2);
061 objectCollection.add(element);
062 }
063 return objectCollection;
064 }
065
066 /**
067 * Expected to be called with a {@link ObjectAdapter} wrapping an array.
068 */
069 @Override
070 public ObjectAdapter firstElement(final ObjectAdapter collectionAdapter) {
071 final Object[] array = array(collectionAdapter);
072 return array.length > 0 ? getAdapterMap().getAdapterFor(array[0]) : null;
073 }
074
075 /**
076 * Expected to be called with a {@link ObjectAdapter} wrapping an array.
077 */
078 @Override
079 public int size(final ObjectAdapter collectionAdapter) {
080 return array(collectionAdapter).length;
081 }
082
083 private Object[] array(final ObjectAdapter collectionAdapter) {
084 return (Object[]) collectionAdapter.getObject();
085 }
086
087 // /////////////////////////////////////////////////////
088 // Dependencies (from constructor)
089 // /////////////////////////////////////////////////////
090
091 private AdapterMap getAdapterMap() {
092 return adapterMap;
093 }
094
095 }