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.progmodel.facets.collections.collection; 021 022import java.util.ArrayList; 023import java.util.Collection; 024 025import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 026import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager; 027import org.apache.isis.core.metamodel.facetapi.FacetHolder; 028import org.apache.isis.core.metamodel.facets.collections.CollectionFacetAbstract; 029 030public class JavaArrayFacet extends CollectionFacetAbstract { 031 032 private final AdapterManager adapterManager; 033 034 public JavaArrayFacet(final FacetHolder holder, final AdapterManager adapterManager) { 035 super(holder); 036 this.adapterManager = 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 = getAdapterManager().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 ? getAdapterManager().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 AdapterManager getAdapterManager() { 092 return adapterManager; 093 } 094 095}