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.util.Objects;
020import java.util.logging.Logger;
021
022/**
023 * Contains information about a feature and can be stored in the feature map
024 * in a {@link Dataset}.
025 */
026public abstract class SkeletalVariableInfo implements VariableInfo {
027    private static final long serialVersionUID = 2L;
028
029    private static final Logger logger = Logger.getLogger(SkeletalVariableInfo.class.getName());
030
031    /**
032     * The name of the feature.
033     */
034    protected final String name;
035    
036    /**
037     * How often the feature occurs in the dataset.
038     */
039    protected int count;
040
041    /**
042     * Constructs a variable info with the supplied name.
043     * @param name The feature name.
044     */
045    protected SkeletalVariableInfo(String name) {
046        this.name = name;
047    }
048
049    /**
050     * Constructs a variable info with the supplied name and initial count.
051     * @param name The feature name.
052     * @param count The initial occurrence count.
053     */
054    protected SkeletalVariableInfo(String name, int count) {
055        this.name = name;
056        this.count = count;
057    }
058
059    /**
060     * Records the value.
061     * @param value The observed value.
062     */
063    protected void observe(double value) {
064        count++;
065    }
066
067    /**
068     * Returns the name of the feature.
069     * @return The name of the feature.
070     */
071    @Override
072    public String getName() {
073        return name;
074    }
075
076    /**
077     * Returns the occurrence count of this feature.
078     * @return The count of observed values.
079     */
080    @Override
081    public int getCount() {
082        return count;
083    }
084
085    @Override
086    public String toString() {
087        return "Feature(name="+name+",count="+count+")";
088    }
089
090    @Override
091    public int hashCode() {
092        int hash = 3;
093        hash = 37 * hash + this.name.hashCode();
094        return hash;
095    }
096
097    @Override
098    public boolean equals(Object obj) {
099        if (this == obj) {
100            return true;
101        }
102        if (obj == null) {
103            return false;
104        }
105        if (getClass() != obj.getClass()) {
106            return false;
107        }
108        final SkeletalVariableInfo other = (SkeletalVariableInfo) obj;
109        return Objects.equals(this.name, other.name);
110    }
111}