Skip to content

Commit

Permalink
Rework static instances to have companion objects extend `EncoderDeco…
Browse files Browse the repository at this point in the history
…der` (#122)
  • Loading branch information
05nelsonm authored Jun 3, 2023
1 parent b326530 commit c167e34
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 108 deletions.
3 changes: 1 addition & 2 deletions library/encoding-base16/api/encoding-base16.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ public final class io/matthewnelson/encoding/base16/Base16 : io/matthewnelson/en
public static final field CHARS_LOWER Ljava/lang/String;
public static final field CHARS_UPPER Ljava/lang/String;
public static final field Companion Lio/matthewnelson/encoding/base16/Base16$Companion;
public static final field INSTANCE Lio/matthewnelson/encoding/base16/Base16;
public fun <init> (Lio/matthewnelson/encoding/base16/Base16$Config;)V
}

public final class io/matthewnelson/encoding/base16/Base16$Companion {
public final class io/matthewnelson/encoding/base16/Base16$Companion : io/matthewnelson/encoding/core/EncoderDecoder {
}

public final class io/matthewnelson/encoding/base16/Base16$Config : io/matthewnelson/encoding/core/EncoderDecoder$Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ import kotlin.jvm.JvmSynthetic
* val bytes = text.encodeToByteArray()
* val encoded = bytes.encodeToString(base16)
* println(encoded) // 48656c6c6f20576f726c6421
* val decoded = encoded.decodeToByteArray(base16).decodeToString()
*
* // Alternatively, use the static implementaton instead of
* // configuring your own settings.
* val decoded = encoded.decodeToByteArray(Base16).decodeToString()
* assertEquals(text, decoded)
*
* @see [io.matthewnelson.encoding.builders.Base16]
* @see [Base16.Config]
* @see [Base16.CHARS_UPPER]
* @see [Base16.CHARS_LOWER]
* @see [Base16.INSTANCE]
* @see [Base16.Companion]
* @see [EncoderDecoder]
* @see [Decoder.decodeToByteArray]
* @see [Decoder.decodeToByteArrayOrNull]
Expand Down Expand Up @@ -116,7 +119,22 @@ public class Base16(config: Base16.Config): EncoderDecoder<Base16.Config>(config
}
}

public companion object {
/**
* Doubles as a static implementation with default settings
* and a lineBreakInterval of 64.
*
* e.g.
*
* val encoded = "Hello World!"
* .encodeToByteArray()
* .encodeToString(Base16)
*
* println(encoded) // 48656c6c6f20576f726c6421
*
* */
public companion object: EncoderDecoder<Base16.Config>(
config = Base16ConfigBuilder().apply { lineBreakInterval = 64 }.build()
) {

/**
* Uppercase Base16 encoding characters.
Expand All @@ -128,11 +146,14 @@ public class Base16(config: Base16.Config): EncoderDecoder<Base16.Config>(config
* */
public const val CHARS_LOWER: String = "0123456789abcdef"

/**
* A static instance with a lineBreakInterval of 64
* */
@JvmField
public val INSTANCE: Base16 = Base16 { lineBreakInterval = 64 }
private val DELEGATE = Base16(config)
protected override fun name(): String = DELEGATE.name()
protected override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Base16.Config>.Feed {
return DELEGATE.newDecoderFeedProtected(out)
}
protected override fun newEncoderFeedProtected(out: OutFeed): Encoder<Base16.Config>.Feed {
return DELEGATE.newEncoderFeedProtected(out)
}
}

protected override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Config>.Feed {
Expand Down
9 changes: 3 additions & 6 deletions library/encoding-base32/api/encoding-base32.api
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ public final class io/matthewnelson/encoding/base32/Base32$Crockford : io/matthe
public static final field CHARS_LOWER Ljava/lang/String;
public static final field CHARS_UPPER Ljava/lang/String;
public static final field Companion Lio/matthewnelson/encoding/base32/Base32$Crockford$Companion;
public static final field INSTANCE Lio/matthewnelson/encoding/base32/Base32$Crockford;
public fun <init> (Lio/matthewnelson/encoding/base32/Base32$Crockford$Config;)V
}

public final class io/matthewnelson/encoding/base32/Base32$Crockford$Companion {
public final class io/matthewnelson/encoding/base32/Base32$Crockford$Companion : io/matthewnelson/encoding/core/EncoderDecoder {
}

public final class io/matthewnelson/encoding/base32/Base32$Crockford$Config : io/matthewnelson/encoding/core/EncoderDecoder$Config {
Expand All @@ -86,11 +85,10 @@ public final class io/matthewnelson/encoding/base32/Base32$Default : io/matthewn
public static final field CHARS_LOWER Ljava/lang/String;
public static final field CHARS_UPPER Ljava/lang/String;
public static final field Companion Lio/matthewnelson/encoding/base32/Base32$Default$Companion;
public static final field INSTANCE Lio/matthewnelson/encoding/base32/Base32$Default;
public fun <init> (Lio/matthewnelson/encoding/base32/Base32$Default$Config;)V
}

public final class io/matthewnelson/encoding/base32/Base32$Default$Companion {
public final class io/matthewnelson/encoding/base32/Base32$Default$Companion : io/matthewnelson/encoding/core/EncoderDecoder {
}

public final class io/matthewnelson/encoding/base32/Base32$Default$Config : io/matthewnelson/encoding/core/EncoderDecoder$Config {
Expand All @@ -103,11 +101,10 @@ public final class io/matthewnelson/encoding/base32/Base32$Hex : io/matthewnelso
public static final field CHARS_LOWER Ljava/lang/String;
public static final field CHARS_UPPER Ljava/lang/String;
public static final field Companion Lio/matthewnelson/encoding/base32/Base32$Hex$Companion;
public static final field INSTANCE Lio/matthewnelson/encoding/base32/Base32$Hex;
public fun <init> (Lio/matthewnelson/encoding/base32/Base32$Hex$Config;)V
}

public final class io/matthewnelson/encoding/base32/Base32$Hex$Companion {
public final class io/matthewnelson/encoding/base32/Base32$Hex$Companion : io/matthewnelson/encoding/core/EncoderDecoder {
}

public final class io/matthewnelson/encoding/base32/Base32$Hex$Config : io/matthewnelson/encoding/core/EncoderDecoder$Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
* val bytes = text.encodeToByteArray()
* val encoded = bytes.encodeToString(base32Crockford)
* println(encoded) // 91JPR-V3F41-BPYWK-CCGGG~
* val decoded = encoded.decodeToByteArray(base32Crockford).decodeToString()
*
* // Alternatively, use the static implementaton instead of
* // configuring your own settings.
* val decoded = encoded.decodeToByteArray(Base32.Crockford).decodeToString()
* assertEquals(text, decoded)
*
* @see [Base32Crockford]
* @see [Crockford.Config]
* @see [Crockford.CHARS_UPPER]
* @see [Crockford.CHARS_LOWER]
* @see [Crockford.INSTANCE]
* @see [Crockford.Companion]
* @see [EncoderDecoder]
* */
public class Crockford(config: Crockford.Config): Base32<Crockford.Config>(config) {
Expand Down Expand Up @@ -192,7 +195,22 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
}
}

public companion object {
/**
* Doubles as a static implementation with default settings
* and a hyphenInterval of 4.
*
* e.g.
*
* val encoded = "Hello World!"
* .encodeToByteArray()
* .encodeToString(Base32.Crockford)
*
* println(encoded) // 91JP-RV3F-41BP-YWKC-CGGG
*
* */
public companion object: EncoderDecoder<Base32.Crockford.Config>(
config = Base32CrockfordConfigBuilder().apply { hyphenInterval = 4 }.build()
) {

/**
* Uppercase Base32 Crockford encoding characters.
Expand All @@ -204,11 +222,14 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
* */
public const val CHARS_LOWER: String = "0123456789abcdefghjkmnpqrstvwxyz"

/**
* A static instance with a hyphenInterval of 4
* */
@JvmField
public val INSTANCE: Base32.Crockford = Base32Crockford { hyphenInterval = 4 }
private val DELEGATE = Crockford(config)
protected override fun name(): String = DELEGATE.name()
protected override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Crockford.Config>.Feed {
return DELEGATE.newDecoderFeedProtected(out)
}
protected override fun newEncoderFeedProtected(out: OutFeed): Encoder<Crockford.Config>.Feed {
return DELEGATE.newEncoderFeedProtected(out)
}
}

protected override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Crockford.Config>.Feed {
Expand Down Expand Up @@ -422,14 +443,17 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
* val bytes = text.encodeToByteArray()
* val encoded = bytes.encodeToString(base32Default)
* println(encoded) // JBSWY3DPEBLW64TMMQQQ====
* val decoded = encoded.decodeToByteArray(base32Default).decodeToString()
*
* // Alternatively, use the static implementaton instead of
* // configuring your own settings.
* val decoded = encoded.decodeToByteArray(Base32.Default).decodeToString()
* assertEquals(text, decoded)
*
* @see [Base32Default]
* @see [Default.Config]
* @see [Default.CHARS_UPPER]
* @see [Default.CHARS_LOWER]
* @see [Default.INSTANCE]
* @see [Default.Companion]
* @see [EncoderDecoder]
* */
public class Default(config: Default.Config): Base32<Default.Config>(config) {
Expand Down Expand Up @@ -488,7 +512,22 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
}
}

public companion object {
/**
* Doubles as a static implementation with default settings
* and a lineBreakInterval of 64.
*
* e.g.
*
* val encoded = "Hello World!"
* .encodeToByteArray()
* .encodeToString(Base32.Default)
*
* println(encoded) // JBSWY3DPEBLW64TMMQQQ====
*
* */
public companion object: EncoderDecoder<Base32.Default.Config>(
config = Base32DefaultConfigBuilder().apply { lineBreakInterval = 64 }.build()
) {

/**
* Uppercase Base32 Default encoding characters.
Expand All @@ -500,11 +539,14 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
* */
public const val CHARS_LOWER: String = "abcdefghijklmnopqrstuvwxyz234567"

/**
* A static instance with a lineBreakInterval of 64
* */
@JvmField
public val INSTANCE: Base32.Default = Base32Default { lineBreakInterval = 64 }
private val DELEGATE = Default(config)
protected override fun name(): String = DELEGATE.name()
protected override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Default.Config>.Feed {
return DELEGATE.newDecoderFeedProtected(out)
}
protected override fun newEncoderFeedProtected(out: OutFeed): Encoder<Default.Config>.Feed {
return DELEGATE.newEncoderFeedProtected(out)
}
}

protected override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Default.Config>.Feed {
Expand Down Expand Up @@ -597,14 +639,17 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
* val bytes = text.encodeToByteArray()
* val encoded = bytes.encodeToString(base32Hex)
* println(encoded) // 91IMOR3F41BMUSJCCGGG====
* val decoded = encoded.decodeToByteArray(base32Hex).decodeToString()
*
* // Alternatively, use the static implementaton instead of
* // configuring your own settings.
* val decoded = encoded.decodeToByteArray(Base32.Hex).decodeToString()
* assertEquals(text, decoded)
*
* @see [Base32Hex]
* @see [Hex.Config]
* @see [Hex.CHARS_UPPER]
* @see [Hex.CHARS_LOWER]
* @see [Hex.INSTANCE]
* @see [Hex.Companion]
* @see [EncoderDecoder]
* */
public class Hex(config: Hex.Config): Base32<Hex.Config>(config) {
Expand Down Expand Up @@ -663,7 +708,22 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
}
}

public companion object {
/**
* Doubles as a static implementation with default settings
* and a lineBreakInterval of 64.
*
* e.g.
*
* val encoded = "Hello World!"
* .encodeToByteArray()
* .encodeToString(Base32.Hex)
*
* println(encoded) // 91IMOR3F41BMUSJCCGGG====
*
* */
public companion object: EncoderDecoder<Base32.Hex.Config>(
config = Base32HexConfigBuilder().apply { lineBreakInterval = 64 }.build()
) {

/**
* Uppercase Base32 Hex encoding characters.
Expand All @@ -675,11 +735,14 @@ public sealed class Base32<C: EncoderDecoder.Config>(config: C): EncoderDecoder<
* */
public const val CHARS_LOWER: String = "0123456789abcdefghijklmnopqrstuv"

/**
* A static instance with a lineBreakInterval of 64
* */
@JvmField
public val INSTANCE: Base32.Hex = Base32Hex { lineBreakInterval = 64 }
private val DELEGATE = Hex(config)
override fun name(): String = DELEGATE.name()
override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Hex.Config>.Feed {
return DELEGATE.newDecoderFeedProtected(out)
}
override fun newEncoderFeedProtected(out: OutFeed): Encoder<Hex.Config>.Feed {
return DELEGATE.newEncoderFeedProtected(out)
}
}

protected override fun newDecoderFeedProtected(out: Decoder.OutFeed): Decoder<Hex.Config>.Feed {
Expand Down
6 changes: 2 additions & 4 deletions library/encoding-base64/api/encoding-base64.api
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ public final class io/matthewnelson/encoding/base64/Base64$Config : io/matthewne
public synthetic fun <init> (ZBZZLkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/matthewnelson/encoding/base64/Base64$Default {
public final class io/matthewnelson/encoding/base64/Base64$Default : io/matthewnelson/encoding/core/EncoderDecoder {
public static final field CHARS Ljava/lang/String;
public static final field INSTANCE Lio/matthewnelson/encoding/base64/Base64$Default;
public static final field INSTANCE Lio/matthewnelson/encoding/base64/Base64;
}

public final class io/matthewnelson/encoding/base64/Base64$UrlSafe {
public final class io/matthewnelson/encoding/base64/Base64$UrlSafe : io/matthewnelson/encoding/core/EncoderDecoder {
public static final field CHARS Ljava/lang/String;
public static final field INSTANCE Lio/matthewnelson/encoding/base64/Base64$UrlSafe;
public static final field INSTANCE Lio/matthewnelson/encoding/base64/Base64;
}

public final class io/matthewnelson/encoding/builders/Base64BuildersKt {
Expand Down
Loading

0 comments on commit c167e34

Please sign in to comment.