@@ -107,16 +107,7 @@ public static int[] convertToIntArray(List<Integer> integers) {
107
107
*/
108
108
public static int [] trits (final String trytes , int length ) {
109
109
int [] trits = trits (trytes );
110
-
111
- List <Integer > tritsList = new LinkedList <>();
112
-
113
- for (int i : trits )
114
- tritsList .add (i );
115
-
116
- while (tritsList .size () < length )
117
- tritsList .add (0 );
118
-
119
- return convertToIntArray (tritsList );
110
+ return Arrays .copyOf (trits , length );
120
111
}
121
112
122
113
/**
@@ -127,17 +118,8 @@ public static int[] trits(final String trytes, int length) {
127
118
* @return A trits array.
128
119
*/
129
120
public static int [] trits (final long trytes , int length ) {
130
- int [] trits = trits (String .valueOf (trytes ));
131
-
132
- List <Integer > tritsList = new LinkedList <>();
133
-
134
- for (int i : trits )
135
- tritsList .add (i );
136
-
137
- while (tritsList .size () < length )
138
- tritsList .add (0 );
139
-
140
- return convertToIntArray (tritsList );
121
+ int [] trits = trits (trytes );
122
+ return Arrays .copyOf (trits , length );
141
123
}
142
124
143
125
/**
@@ -146,12 +128,9 @@ public static int[] trits(final long trytes, int length) {
146
128
* @param trytes The trytes.
147
129
* @return A trits array.
148
130
*/
131
+ @ Deprecated
149
132
public static int [] tritsString (final String trytes ) {
150
- int [] d = new int [3 * trytes .length ()];
151
- for (int i = 0 ; i < trytes .length (); i ++) {
152
- System .arraycopy (TRYTE_TO_TRITS_MAPPINGS [Constants .TRYTE_ALPHABET .indexOf (trytes .charAt (i ))], 0 , d , i * NUMBER_OF_TRITS_IN_A_TRYTE , NUMBER_OF_TRITS_IN_A_TRYTE );
153
- }
154
- return d ;
133
+ return trits (trytes );
155
134
}
156
135
157
136
/**
@@ -160,43 +139,46 @@ public static int[] tritsString(final String trytes) {
160
139
* @param trytes The trytes to be converted.
161
140
* @return Array of trits.
162
141
**/
163
- public static int [] trits (final String trytes ) {
142
+ public static int [] trits (final long trytes ) {
164
143
final List <Integer > trits = new LinkedList <>();
165
- if (InputValidator .isValue (trytes )) {
166
-
167
- long value = Long .parseLong (trytes );
168
-
169
- long absoluteValue = value < 0 ? -value : value ;
170
-
171
- int position = 0 ;
144
+ long absoluteValue = trytes < 0 ? -trytes : trytes ;
172
145
173
- while ( absoluteValue > 0 ) {
146
+ int position = 0 ;
174
147
175
- int remainder = (int ) (absoluteValue % RADIX );
176
- absoluteValue /= RADIX ;
148
+ while (absoluteValue > 0 ) {
177
149
178
- if (remainder > MAX_TRIT_VALUE ) {
179
- remainder = MIN_TRIT_VALUE ;
180
- absoluteValue ++;
181
- }
150
+ int remainder = (int ) (absoluteValue % RADIX );
151
+ absoluteValue /= RADIX ;
182
152
183
- trits .add (position ++, remainder );
153
+ if (remainder > MAX_TRIT_VALUE ) {
154
+ remainder = MIN_TRIT_VALUE ;
155
+ absoluteValue ++;
184
156
}
185
- if (value < 0 ) {
186
- for (int i = 0 ; i < trits .size (); i ++) {
187
- trits .set (i , -trits .get (i ));
188
- }
189
- }
190
- } else {
191
- int [] d = new int [3 * trytes .length ()];
192
- for (int i = 0 ; i < trytes .length (); i ++) {
193
- System .arraycopy (TRYTE_TO_TRITS_MAPPINGS [Constants .TRYTE_ALPHABET .indexOf (trytes .charAt (i ))], 0 , d , i * NUMBER_OF_TRITS_IN_A_TRYTE , NUMBER_OF_TRITS_IN_A_TRYTE );
157
+
158
+ trits .add (position ++, remainder );
159
+ }
160
+ if (trytes < 0 ) {
161
+ for (int i = 0 ; i < trits .size (); i ++) {
162
+ trits .set (i , -trits .get (i ));
194
163
}
195
- return d ;
196
164
}
197
165
return convertToIntArray (trits );
198
166
}
199
167
168
+ /**
169
+ * Converts trytes into trits.
170
+ *
171
+ * @param trytes The trytes to be converted.
172
+ * @return Array of trits.
173
+ **/
174
+ public static int [] trits (final String trytes ) {
175
+ int [] d = new int [3 * trytes .length ()];
176
+ for (int i = 0 ; i < trytes .length (); i ++) {
177
+ System .arraycopy (TRYTE_TO_TRITS_MAPPINGS [Constants .TRYTE_ALPHABET .indexOf (trytes .charAt (i ))], 0 , d , i * NUMBER_OF_TRITS_IN_A_TRYTE , NUMBER_OF_TRITS_IN_A_TRYTE );
178
+ }
179
+ return d ;
180
+ }
181
+
200
182
/**
201
183
* Copies the trits from the input string into the destination array
202
184
*
@@ -299,4 +281,4 @@ public static void increment(final int[] trits, final int size) {
299
281
}
300
282
}
301
283
302
- }
284
+ }
0 commit comments