org.xlcloud.console.entitlements.utils.EntitlementDictionaryCollectionUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.xlcloud.console.entitlements.utils.EntitlementDictionaryCollectionUtil.java

Source

/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.console.entitlements.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import org.primefaces.model.TreeNode;
import org.xlcloud.console.entitlements.ExplicitEntitlementDictionary;

/**
 * Utils class which is used to collect explicit entitlement dictionaries
 *  from the array of tree nodes.
 * It expects that entitlement dictionaries are wrapped by TreeNode objects.
 *
 * @author Konrad Krl, AMG.net
 */
public class EntitlementDictionaryCollectionUtil {

    /**
     * Collects explicit entitlement dictionaries wrapped by TreeNode objects
     * @param treeNodes tree nodes
     * @return the list of explicit entitlement dictionaries
     */
    @SuppressWarnings("unchecked")
    public static List<ExplicitEntitlementDictionary> collectExplicitEntitlementDictionaries(TreeNode treeNodes[]) {
        List<ExplicitEntitlementDictionary> explicitEntitlements = new ArrayList<ExplicitEntitlementDictionary>();

        Collection<TreeNode> leafNodes = CollectionUtils.select(Arrays.asList(treeNodes), new Predicate() {
            @Override
            public boolean evaluate(Object object) {
                TreeNode node = (TreeNode) object;
                return (node.getData() instanceof ExplicitEntitlementDictionary);
            }
        });

        CollectionUtils.collect(leafNodes, new Transformer() {
            @Override
            public Object transform(Object input) {
                TreeNode node = (TreeNode) input;
                return (ExplicitEntitlementDictionary) node.getData();
            }
        }, explicitEntitlements);

        return explicitEntitlements;
    }
}