001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.jdbc.rest;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022import org.codehaus.jettison.json.JSONException;
023import org.codehaus.jettison.json.JSONObject;
024import org.modeshape.common.annotation.Immutable;
025
026/**
027 * A collection of {@link NodeType} returned by the client
028 */
029@Immutable
030public final class NodeTypes implements Iterable<NodeType> {
031
032    private final Map<String, NodeType> typesByName = new HashMap<>();
033
034    @SuppressWarnings("unchecked")
035    protected NodeTypes(JSONObject object) {
036        try {
037            if (!object.has("children")) {
038                throw new RuntimeException("Invalid JSON object: " + object);
039            }
040            JSONObject children = object.getJSONObject("children");
041            for (Iterator<String> itr = children.keys(); itr.hasNext(); ) {
042                String key = itr.next();
043                JSONObject child = children.getJSONObject(key);
044                if (child != null) {
045                    // and that means that each child is a JSONObject ...
046                    NodeType nodeType = new NodeType(child, this);
047                    typesByName.put(nodeType.getName(), nodeType);
048                }
049            }
050        } catch (JSONException e) {
051            throw new RuntimeException(e);
052        }
053    }
054
055    protected NodeType getNodeType( String name ) {
056        return typesByName.get(name);
057    }
058
059    @Override
060    public Iterator<NodeType> iterator() {
061        return nodeTypes().iterator();
062    }
063
064    /**
065     * Checks if there are any node types registered.
066     *
067     * @return {@code true} if there are any node types, {@code false} otherwise.
068     */
069    public boolean isEmpty() {
070        return typesByName.isEmpty();
071    }
072
073    protected Collection<NodeType> nodeTypes() {
074        return typesByName.values();
075    }
076
077    protected NodeType[] toNodeTypes( Collection<String> nodeTypeNames ) {
078        if (this.typesByName.isEmpty()) {
079            return new NodeType[0];
080        }
081        int numValues = nodeTypeNames.size();
082        int i = 0;
083        NodeType[] result = new NodeType[numValues];
084        for (String typeName : nodeTypeNames) {
085            result[i++] = getNodeType(typeName);
086        }
087        return result;
088    }
089
090}