Skip to content

Commit

Permalink
Merge pull request #27 from filip26/patch/v0.0.2
Browse files Browse the repository at this point in the history
Patch/v0.0.2
  • Loading branch information
filip26 authored Nov 26, 2023
2 parents 61864b9 + d45a6fc commit e0d1e9d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.apicatalog</groupId>
<artifactId>copper-multibase</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>jar</packaging>

<name>Copper Multibase</name>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/apicatalog/multibase/Multibase.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public int length() {
return length;
}

/**
* Checks if the given value is encoded with the base.
*
* @param encoded an encoded value to test
* @return <code>true</code> is the given value is encoded with the base,
* <code>false</code> otherwise
*/
public boolean isEncoded(final String encoded) {
return encoded != null
&& encoded.trim().length() > 0
&& prefix == encoded.charAt(0);
}

/**
* Decodes the given data into byte array.
*
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/apicatalog/multibase/MultibaseDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@ protected MultibaseDecoder(Map<Character, Multibase> bases) {
this.bases = bases;
}

/**
* A new instance initialized with all supported bases.
*
* @return a new instance
*/
public static MultibaseDecoder getInstance() {
return getInstance(Multibase.BASE_58_BTC);
}

/**
* A new instance initialized with the given bases.
*
* @param bases to initialize the decoder
* @return a new instance
*/
public static MultibaseDecoder getInstance(Multibase... bases) {
return new MultibaseDecoder(
Arrays.stream(bases)
.collect(Collectors.toMap(Multibase::prefix, Function.identity())));
}

/**
* Tries to detect the base used for encoding.
*
* @param encoded an encoded value
* @return detected base encoding or {@link Optional#empty()}
*/
public Optional<Multibase> getBase(final String encoded) {

if (encoded == null) {
Expand Down
23 changes: 18 additions & 5 deletions src/test/java/com/apicatalog/multibase/MultibaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -19,19 +22,29 @@ void testBase58BTCEncode(String expected, byte[] data) {
String output = Multibase.BASE_58_BTC.encode(data);
assertEquals(expected, output);
}

@ParameterizedTest(name = "{index}: {0}")
@MethodSource("testData")
void testDecode(String encoded, byte[] expected) {
byte[] output = DECODER.decode(encoded);
assertArrayEquals(expected, output);
}

@ParameterizedTest(name = "{index}: {0}")
@MethodSource("testData")
void testIsBase58BTCEncoded(String encoded, byte[] data) {
assertTrue(Multibase.BASE_58_BTC.isEncoded(encoded));
}

@Test
void testIsNotBase58BTCEncoded() {
assertFalse(Multibase.BASE_58_BTC.isEncoded("abc"));
}

static Stream<Arguments> testData() {
return Stream.of(
Arguments.of("zUXE7GvtEk8XTXs1GF8HSGbVA9FCX9SEBPe", "Decentralize everything!!".getBytes()),
Arguments.of("zStV1DL6CwTryKyV", "hello world".getBytes()),
Arguments.of("z7paNL19xttacUY", "yes mani !".getBytes())
);
Arguments.of("zUXE7GvtEk8XTXs1GF8HSGbVA9FCX9SEBPe", "Decentralize everything!!".getBytes()),
Arguments.of("zStV1DL6CwTryKyV", "hello world".getBytes()),
Arguments.of("z7paNL19xttacUY", "yes mani !".getBytes()));
}
}

0 comments on commit e0d1e9d

Please sign in to comment.