OSDN Git Service

7fc6df736a9a9f35ae8d45a2c6b22dbb3663249d
[stigmata/stigmata-plugins.git] / kgram / 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(
65                 "expected 0-" + (maxLength - 1) + ": " + index
66             );
67         }
68         if(value == null){
69             throw new NullPointerException("null value");
70         }
71         if(values == null){
72             values = (T[])Array.newInstance(value.getClass(), getKValue());
73         }
74         values[index] = value;
75     }
76
77     /**
78      * returns an object of given index.
79      */
80     public T get(int index){
81         T returnValue = null;
82         if(index < 0 || index >= maxLength){
83             throw new ArrayIndexOutOfBoundsException(
84                 "expected 0-" + (maxLength - 1) + ": " + index
85             );
86         }
87         if(values != null){
88             returnValue = values[index];
89         }
90
91         return returnValue;
92     }
93
94     /**
95      * adds value at last index.
96      * 
97      * this object is called with given 2 when following situation, 
98      * <ul>
99      *   <li>{ 1, 3, null, null } -&gt; { 1, 2, 3, null } and return 2<li>
100      *   <li>{ 1, null, 3, null } -&gt; { 1, 2, 3, null } and return 1<li>
101      *   <li>{ 1, 2, 3, 4 } -&gt; { 1, 2, 3, 4 } and return -1<li>
102      * </ul>
103      * 
104      * @param value value for addition.
105      * @return added index.
106      */
107     public int add(T value){
108         int index = -1;
109         for(int i = 0; i < values.length; i++){
110             if(values[i] == null){
111                 index = i;
112                 values[i] = value;
113                 break;
114             }
115         }
116         return index;
117     }
118
119     /**
120      * returns an array of elements this object has.
121      * @return
122      */
123     @SuppressWarnings("unchecked")
124     public T[] toArray(){
125         if(values == null){
126             throw new IllegalStateException("this object has no elements.");
127         }
128         T[] newarray = (T[])Array.newInstance(
129             values[0].getClass(), getKValue()
130         );
131         System.arraycopy(values, 0, newarray, 0, getKValue());
132         return newarray;
133     }
134
135     @Override
136     public boolean equals(Object o){
137         if(o instanceof KGram){
138             KGram<?> kgram = (KGram<?>)o;
139             boolean flag = getKValue() == kgram.getKValue();
140             for(int i = 0; !flag && i < maxLength; i++){
141                 if(!get(i).equals(kgram.get(i))){
142                     flag = false;
143                     break;
144                 }
145             }
146             return flag;
147         }
148         return false;
149     }
150
151     @Override
152     public int hashCode(){
153         return Arrays.hashCode(values);
154     }
155 }