001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.pojotools;
014
015import java.lang.reflect.*;
016import java.util.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.internal.*;
020
021/**
022 * Designed to provide paging on POJOs consisting of arrays and collections.
023 *
024 * <p>
025 * Allows you to quickly return subsets of arrays and collections based on position/limit arguments.
026 */
027@SuppressWarnings({"unchecked","rawtypes"})
028public final class PojoViewer implements PojoTool<ViewArgs> {
029
030   @Override /* PojoTool */
031   public Object run(BeanSession session, Object input, ViewArgs args) {
032
033      if (input == null)
034         return null;
035
036      List<String> view = args.getView();
037      ClassMeta type = session.getClassMetaForObject(input);
038
039      if (type.isBeanMap())
040         return new DelegateBeanMap(((BeanMap)input).getBean(), session).filterKeys(view);
041      if (type.isMap())
042         return new DelegateMap((Map)input, session).filterKeys(view);
043      if (type.isBean())
044         return new DelegateBeanMap(input, session).filterKeys(view);
045
046      ArrayList<Object> l = null;
047
048      if (type.isArray()) {
049         int size = Array.getLength(input);
050         l = new ArrayList<>(size);
051         for (int i = 0; i < size; i++)
052            l.add(Array.get(input, i));
053      } else if (type.isCollection()) {
054         Collection c = (Collection)input;
055         l = new ArrayList<>(c.size());
056         for (Object o : c)
057            l.add(o);
058      } else {
059         return input;
060      }
061
062      for (ListIterator li = l.listIterator(); li.hasNext();) {
063         Object o = li.next();
064         ClassMeta cm2 = session.getClassMetaForObject(o);
065
066         if (cm2 == null)
067            o = null;
068         else if (cm2.isBeanMap())
069            o = new DelegateBeanMap(((BeanMap)o).getBean(), session).filterKeys(view);
070         else if (cm2.isMap())
071            o = new DelegateMap((Map)o, session).filterKeys(view);
072         else if (cm2.isBean())
073            o = new DelegateBeanMap(o, session).filterKeys(view);
074
075         li.set(o);
076      }
077
078      return l;
079   }
080}