001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
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 implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Iterator;
022import java.util.Map;
023import java.util.Set;
024import java.util.TreeMap;
025
026/**
027 * A map from Strings to {@link VariableInfo} objects storing
028 * information about a feature.
029 */
030public abstract class FeatureMap implements Serializable, Iterable<VariableInfo> {
031    private static final long serialVersionUID = 1L;
032
033    /**
034     * Map from the feature names to their info.
035     */
036    protected final Map<String, VariableInfo> m;
037
038    /**
039     * Constructs an empty feature map.
040     */
041    protected FeatureMap() {
042        m = new HashMap<>();
043    }
044
045    /**
046     * Constructs a deep copy of the supplied feature map.
047     * @param map The map to copy.
048     */
049    protected FeatureMap(FeatureMap map) {
050        m = new HashMap<>();
051        for (Map.Entry<String,VariableInfo> e : map.m.entrySet()) {
052            VariableInfo info = e.getValue().copy();
053
054            m.put(e.getKey(),info);
055        }
056    }
057
058    /**
059     * Constructs a feature map wrapping the supplied map.
060     * <p>
061     * Note the map is not defensively copied.
062     * @param m The map to wrap.
063     */
064    @SuppressWarnings("unchecked") // upcasting off the wildcard.
065    protected FeatureMap(Map<String, ? extends VariableInfo> m) {
066        this.m = (Map<String,VariableInfo>) m;
067    }
068
069    /**
070     * Gets the variable info associated with that feature name, or null if it's unknown.
071     * @param name The feature name.
072     * @return The variable info or null.
073     */
074    public VariableInfo get(String name) {
075        return m.get(name);
076    }
077
078    /**
079     * Returns the number of features in the domain.
080     * @return The number of features.
081     */
082    public int size() {
083        return m.size();
084    }
085
086    @Override
087    public String toString() {
088        return m.toString();
089    }
090
091    /**
092     * Returns all the feature names in the domain.
093     * @return The feature names.
094     */
095    public Set<String> keySet() {
096        return m.keySet();
097    }
098
099    @Override
100    public Iterator<VariableInfo> iterator() {
101        return m.values().iterator();
102    }
103
104    /**
105     * Same as the toString, but ordered by name, and with newlines.
106     * @return A String representation of this FeatureMap.
107     */
108    public String toReadableString() {
109        StringBuilder sb = new StringBuilder();
110        TreeMap<String,VariableInfo> tm = new TreeMap<>(m);
111        for (Map.Entry<String, VariableInfo> e : tm.entrySet()) {
112            if(sb.length() > 0) {
113                sb.append('\n');
114            }
115            sb.append(e.getValue().toString());
116        }
117        return sb.toString();
118    }
119
120}