Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit f758fc2

Browse files
authored
Merge pull request #36 from schierlm/patch-2
Fix ambiguous utility method call - thx @schierlm
2 parents b580643 + 8f655c0 commit f758fc2

File tree

1 file changed

+35
-53
lines changed

1 file changed

+35
-53
lines changed

src/main/java/jota/utils/Converter.java

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,7 @@ public static int[] convertToIntArray(List<Integer> integers) {
107107
*/
108108
public static int[] trits(final String trytes, int length) {
109109
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);
120111
}
121112

122113
/**
@@ -127,17 +118,8 @@ public static int[] trits(final String trytes, int length) {
127118
* @return A trits array.
128119
*/
129120
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);
141123
}
142124

143125
/**
@@ -146,12 +128,9 @@ public static int[] trits(final long trytes, int length) {
146128
* @param trytes The trytes.
147129
* @return A trits array.
148130
*/
131+
@Deprecated
149132
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);
155134
}
156135

157136
/**
@@ -160,43 +139,46 @@ public static int[] tritsString(final String trytes) {
160139
* @param trytes The trytes to be converted.
161140
* @return Array of trits.
162141
**/
163-
public static int[] trits(final String trytes) {
142+
public static int[] trits(final long trytes) {
164143
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;
172145

173-
while (absoluteValue > 0) {
146+
int position = 0;
174147

175-
int remainder = (int) (absoluteValue % RADIX);
176-
absoluteValue /= RADIX;
148+
while (absoluteValue > 0) {
177149

178-
if (remainder > MAX_TRIT_VALUE) {
179-
remainder = MIN_TRIT_VALUE;
180-
absoluteValue++;
181-
}
150+
int remainder = (int) (absoluteValue % RADIX);
151+
absoluteValue /= RADIX;
182152

183-
trits.add(position++, remainder);
153+
if (remainder > MAX_TRIT_VALUE) {
154+
remainder = MIN_TRIT_VALUE;
155+
absoluteValue++;
184156
}
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));
194163
}
195-
return d;
196164
}
197165
return convertToIntArray(trits);
198166
}
199167

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+
200182
/**
201183
* Copies the trits from the input string into the destination array
202184
*
@@ -299,4 +281,4 @@ public static void increment(final int[] trits, final int size) {
299281
}
300282
}
301283

302-
}
284+
}

0 commit comments

Comments
 (0)