diff --git a/src/main/java/com/blibli/oss/common/helper/util/NamedNumber.java b/src/main/java/com/blibli/oss/common/helper/util/NamedNumber.java new file mode 100644 index 0000000..74f6282 --- /dev/null +++ b/src/main/java/com/blibli/oss/common/helper/util/NamedNumber.java @@ -0,0 +1,111 @@ +package com.blibli.oss.common.helper.util; + +import java.io.Serializable; + +/** + * Common base class for dynamic named number (enum like). + * Example: + *
+ * public final class HttpStatusCode extends NamedNumber {
+ *
+ *      public static final HttpStatusCode NOT_FOUND =
+ *          new HttpStatusCode(404, "Not found.");
+ *
+ *      public static final HttpStatusCode OK =
+ *          new HttpStatusCode(20, "Ok.");
+ *
+ *      public static final HttpStatusCode UNKNOWN =
+ *          new HttpStatusCode(0, "Unknown Http Status Code.");
+ *
+ *      public HttpStatusCode(Integer value, String name) {
+ *          super(value, name);
+ *      }
+ *
+ *      private static final Map registry
+ *          = new HashMap<>();
+ *
+ *      public static final HttpStatusCode register(final HttpStatusCode httpStatusCode) {
+ *          registry.put(httpStatusCode.getValue(), httpStatusCode);
+ *          return httpStatusCode;
+ *      }
+ *
+ *      public static final HttpStatusCode valueOf(final int rawValue) {
+ *          HttpStatusCode httpStatusCode = registry.get(rawValue);
+ *          if (httpStatusCode == null) {
+ *              return UNKNOWN;
+ *          }
+ *          return httpStatusCode;
+ *      }
+ *
+ *      static {
+ *          registry.put(NOT_FOUND.getValue(), NOT_FOUND);
+ *          registry.put(OK.getValue(), OK);
+ *          registry.put(UNKNOWN.getValue(), UNKNOWN);
+ *      }
+ *
+ *  }
+ *  
+ * @see Enum + * @see Number + * @author Ardika Rommy Sanjaya + * @since 0.0.1 + * @param number. + * @param named number. + */ +public abstract class NamedNumber> implements Serializable { + + private static final long serialVersionUID = -7754849362562086047L; + + private final T value; + private final String name; + + protected NamedNumber(T value, String name) { + this.value = value; + this.name = name; + } + + /** + * Returns the number of this {@code NamedNumber} object. + * @return returns the number of this {@code NamedNumber} object. + */ + public T getValue() { + return this.value; + } + + /** + * Returns the name of this {@code NamedNumber} object. + * @return returns the name of this {@code NamedNumber} object. + */ + public String getName() { + return this.name; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj.getClass() != this.getClass()) { + return false; + } + if (!(obj instanceof NamedNumber)) { + return false; + } + return this.value.equals(this.getClass().cast(obj).getValue()); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public String toString() { + return new StringBuilder("[Value: ") + .append(this.value) + .append(", Name: ") + .append(this.name) + .append("]").toString(); + } + +} diff --git a/src/test/java/com/blibli/oss/common/helper/NamedNumberTest.java b/src/test/java/com/blibli/oss/common/helper/NamedNumberTest.java new file mode 100644 index 0000000..dc2fbb5 --- /dev/null +++ b/src/test/java/com/blibli/oss/common/helper/NamedNumberTest.java @@ -0,0 +1,44 @@ +package com.blibli.oss.common.helper; + +import com.blibli.oss.common.helper.config.HttpStatusCode; +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +/** + * @author Ardika Rommy Sanjaya + */ +public class NamedNumberTest { + + public static final int NOT_FOUND_CODE = 404; + public static final int UNKNOWN_CODE = 10000; + public static final int INTERNAL_SERVER_ERROR_CODE = 500; + + @Test + public void found() { + HttpStatusCode httpStatusCode = HttpStatusCode.valueOf(NOT_FOUND_CODE); + assertEquals(HttpStatusCode.NOT_FOUND.getValue(), httpStatusCode.getValue()); + } + + @Test + public void notFound() { + HttpStatusCode httpStatusCode = HttpStatusCode.valueOf(UNKNOWN_CODE); + assertEquals(HttpStatusCode.UNKNOWN.getValue(), httpStatusCode.getValue()); + } + + @Test + public void registerNewCode() { + /** + * Register http status code. + */ + HttpStatusCode httpStatusCode = new HttpStatusCode(INTERNAL_SERVER_ERROR_CODE, "Internal Server Error.r"); + HttpStatusCode.register(httpStatusCode); + + /** + * Test + */ + HttpStatusCode internalServerError = HttpStatusCode.valueOf(INTERNAL_SERVER_ERROR_CODE); + assertEquals(httpStatusCode.getValue(), internalServerError.getValue()); + } + +} diff --git a/src/test/java/com/blibli/oss/common/helper/config/HttpStatusCode.java b/src/test/java/com/blibli/oss/common/helper/config/HttpStatusCode.java new file mode 100644 index 0000000..e898039 --- /dev/null +++ b/src/test/java/com/blibli/oss/common/helper/config/HttpStatusCode.java @@ -0,0 +1,47 @@ +package com.blibli.oss.common.helper.config; + +import com.blibli.oss.common.helper.util.NamedNumber; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Ardika Rommy Sanjaya + */ +public final class HttpStatusCode extends NamedNumber { + + private static final Map registry + = new HashMap<>(); + + public static final HttpStatusCode NOT_FOUND = + new HttpStatusCode(404, "Not found."); + + public static final HttpStatusCode OK = + new HttpStatusCode(20, "OK."); + + public static final HttpStatusCode UNKNOWN = + new HttpStatusCode(0, "Unknown Http Status Code."); + + public HttpStatusCode(Integer value, String name) { + super(value, name); + } + + public static final HttpStatusCode register(final HttpStatusCode httpStatusCode) { + registry.put(httpStatusCode.getValue(), httpStatusCode); + return httpStatusCode; + } + + public static final HttpStatusCode valueOf(final int rawValue) { + HttpStatusCode httpStatusCode = registry.get(rawValue); + if (httpStatusCode == null) { + return UNKNOWN; + } + return httpStatusCode; + } + + static { + registry.put(NOT_FOUND.getValue(), NOT_FOUND); + registry.put(OK.getValue(), OK); + registry.put(UNKNOWN.getValue(), UNKNOWN); + } + +}