001/** 
002 * Copyright (c) 2007-2008, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.ml;
025
026import java.io.Serializable;
027import java.util.ArrayList;
028import java.util.Collection;
029import java.util.List;
030
031import com.google.common.base.Objects;
032
033/**
034 * <br>
035 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
036 * All rights reserved.
037 * 
038 * 
039 * @author Steven Bethard
040 */
041public class Instance<OUTCOME_TYPE> implements Serializable {
042
043  /**
044   * 
045   */
046  private static final long serialVersionUID = -5115524159642754897L;
047
048  /**
049   * Create a new ClassifierInstance with an empty set of features and a null label.
050   */
051  public Instance() {
052    this.features = new ArrayList<Feature>();
053  }
054
055  public Instance(OUTCOME_TYPE outcome) {
056    this();
057    this.outcome = outcome;
058  }
059
060  public Instance(OUTCOME_TYPE outcome, Collection<Feature> features) {
061    this(outcome);
062    addAll(features);
063  }
064
065  public Instance(Collection<Feature> features) {
066    this();
067    addAll(features);
068  }
069
070  /**
071   * Get the list of features for this instance.
072   * 
073   * @return The list of features
074   */
075  public List<Feature> getFeatures() {
076    return this.features;
077  }
078
079  /**
080   * Add a feature to the instance.
081   * 
082   * @param feature
083   *          The feature to add.
084   */
085  public void add(Feature feature) {
086    this.features.add(feature);
087  }
088
089  /**
090   * Add a collection of features to the instance.
091   * 
092   * @param feats
093   *          The features to add.
094   */
095  public void addAll(Collection<Feature> feats) {
096    this.features.addAll(feats);
097  }
098
099  /**
100   * Get the current label for the instance.
101   * 
102   * @return The current label value.
103   */
104  public OUTCOME_TYPE getOutcome() {
105    return this.outcome;
106  }
107
108  /**
109   * Set the current label for the instance.
110   * 
111   * @param outcome
112   *          The new label value.
113   */
114  public void setOutcome(OUTCOME_TYPE outcome) {
115    this.outcome = outcome;
116  }
117
118  @Override
119  public String toString() {
120    String className = this.getClass().getSimpleName();
121    return String.format("%s(%s, %s)", className, this.outcome, this.features);
122  }
123
124  @Override
125  public boolean equals(Object object) {
126    if (!(object instanceof Instance)) {
127      return false;
128    }
129    Instance<?> that = (Instance<?>) object;
130    return Objects.equal(this.outcome, that.outcome) && Objects.equal(this.features, that.features);
131  }
132
133  @Override
134  public int hashCode() {
135    return Objects.hashCode(this.outcome, this.features);
136  }
137
138  private List<Feature> features;
139
140  private OUTCOME_TYPE outcome = null;
141
142}