OSDN Git Service

Delete Subversion Tags (Revision, Id)
[stigmata/stigmata-core.git] / src / main / java / jp / sourceforge / stigmata / birthmarks / kgram / KGram.java
1 package jp.sourceforge.stigmata.birthmarks.kgram;
2
3 import java.io.Serializable;
4 import java.lang.reflect.Array;
5 import java.util.Arrays;
6
7 /**
8  * This class represents k-gram of the some sequence. 
9  * 
10  * @author Haruaki TAMADA
11  */
12 public class KGram<T> implements Serializable{
13     private static final long serialVersionUID = 273465874532523L;
14     // private List<T> list = new ArrayList<T>();
15     private int maxLength = 4;
16     private T[] values;
17
18     /**
19      * constructor.
20      * @param kvalue the number of elements of this object.
21      */
22     public KGram(int kvalue){
23         setKValue(kvalue);
24     }
25
26     /**
27      * sets k-value. 
28      * @param kvalue the number of elements of this object.
29      */
30     public void setKValue(int kvalue){
31         this.maxLength = kvalue;
32     }
33
34     /**
35      * returns k-value which is the number of elements.
36      * @return the number of elements.
37      */
38     public int getKValue(){
39         return maxLength;
40     }
41
42     /**
43      * returns string representation of this object.
44      */
45     @Override
46     public String toString(){
47         StringBuffer buffer = new StringBuffer("{ ");
48         for(int i = 0; i < maxLength; i++){
49             if(i != 0) buffer.append(", ");
50             buffer.append(get(i));
51         }
52         buffer.append(" }");
53         return new String(buffer);
54     }
55
56     /**
57      * sets the given value to kgram element at given index.
58      * @param index index.
59      * @param value value.
60      */
61     @SuppressWarnings("unchecked")
62     public void set(int index, T value){
63         if(index < 0 || index >= maxLength){
64             throw new ArrayIndexOutOfBoundsException("expected 0-" + (maxLength - 1) + ": " + index);
65         }
66         if(value == null){
67             throw new NullPointerException("null value");
68         }
69         if(values == null){
70             values = (T[])Array.newInstance(value.getClass(), getKValue());
71         }
72         values[index] = value;
73     }
74
75     /**
76      * returns an object of given index.
77      */
78     public T get(int index){
79         T returnValue = null;
80         if(index < 0 || index >= maxLength){
81             throw new ArrayIndexOutOfBoundsException("expected 0-" + (maxLength - 1) + ": " + index);
82         }
83         if(values != null){
84             returnValue = values[index];
85         }
86
87         return returnValue;
88     }
89
90     /**
91      * adds value at last index.
92      * 
93      * this object is called with given 2 when following situation, 
94      * <ul>
95      *   <li>``{ 1, 3, null, null }'' -&gt; ``{ 1, 2, 3, null }'' and return 2<li>
96      *   <li>``{ 1, null, 3, null }'' -&gt; ``{ 1, 2, 3, null }'' and return 1<li>
97      *   <li>``{ 1, 2, 3, 4 }'' -&gt; ``{ 1, 2, 3, 4 }'' and return -1<li>
98      * </ul>
99      * 
100      * @param value value for addition.
101      * @return added index.
102      */
103     public int add(T value){
104         int index = -1;
105         for(int i = 0; i < values.length; i++){
106             if(values[i] == null){
107                 index = i;
108                 values[i] = value;
109                 break;
110             }
111         }
112         return index;
113     }
114
115     /**
116      * returns an array of elements this object has.
117      * @return
118      */
119     @SuppressWarnings("unchecked")
120     public T[] toArray(){
121         if(values == null){
122             throw new IllegalStateException("this object has no elements.");
123         }
124         T[] newarray = (T[])Array.newInstance(values[0].getClass(), getKValue());
125         System.arraycopy(values, 0, newarray, 0, getKValue());
126         return newarray;
127     }
128
129     @Override
130     public boolean equals(Object o){
131         if(o instanceof KGram){
132             KGram<?> kgram = (KGram<?>)o;
133             boolean flag = getKValue() == kgram.getKValue();
134             for(int i = 0; !flag && i < maxLength; i++){
135                 if(!get(i).equals(kgram.get(i))){
136                     flag = false;
137                     break;
138                 }
139             }
140             return flag;
141         }
142         return false;
143     }
144
145     @Override
146     public int hashCode(){
147         return Arrays.hashCode(values);
148     }
149 }