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; 027 028/** 029 * <br> 030 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 031 * All rights reserved. 032 * 033 * <p> 034 * 035 * @author Philip Ogren 036 * @author Philipp Wetzler 037 * @author Steven Bethard 038 */ 039 040public class Feature implements Serializable { 041 042 /** 043 * 044 */ 045 private static final long serialVersionUID = -3215288856677656204L; 046 047 protected String name; 048 049 protected Object value; 050 051 public Feature() { 052 } 053 054 public Feature(Object value) { 055 this.value = value; 056 } 057 058 public Feature(String name, Object value) { 059 this.name = name; 060 this.value = value; 061 } 062 063 public static Feature createFeature(String namePrefix, Feature feature) { 064 return new Feature(createName(namePrefix, feature.name), feature.value); 065 } 066 067 public Object getValue() { 068 return value; 069 } 070 071 public void setValue(Object value) { 072 this.value = value; 073 } 074 075 public String getName() { 076 return name; 077 } 078 079 public void setName(String name) { 080 this.name = name; 081 } 082 083 public static String createName(String... names) { 084 StringBuffer buffer = new StringBuffer(); 085 for (String name : names) { 086 if (name != null) { 087 buffer.append(name); 088 buffer.append('_'); 089 } 090 } 091 if (buffer.length() > 0) { 092 buffer.deleteCharAt(buffer.length() - 1); 093 } 094 return buffer.toString(); 095 } 096 097 public String toString() { 098 String className = Feature.class.getSimpleName(); 099 return String.format("%s(<%s>, <%s>)", className, this.name, this.value); 100 } 101 102 @Override 103 public boolean equals(Object obj) { 104 if (obj instanceof Feature) { 105 Feature other = (Feature) obj; 106 boolean nameMatch = (this.name == null && other.name == null) 107 || (this.name != null && this.name.equals(other.name)); 108 boolean valueMatch = (this.value == null && other.value == null) 109 || (this.value != null && this.value.equals(other.value)); 110 return nameMatch && valueMatch; 111 } else { 112 return false; 113 } 114 } 115 116 @Override 117 public int hashCode() { 118 int hash = 1; 119 hash = hash * 31 + (this.name == null ? 0 : this.name.hashCode()); 120 hash = hash * 31 + (this.value == null ? 0 : this.value.hashCode()); 121 return hash; 122 } 123 124}