Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/main/java/com/blibli/oss/common/helper/util/NamedNumber.java
Original file line number Diff line number Diff line change
@@ -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:
* <pre>
* public final class HttpStatusCode extends NamedNumber<Integer, HttpStatusCode> {
*
* 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<Integer, HttpStatusCode> 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);
* }
*
* }
* </pre>
* @see Enum
* @see Number
* @author Ardika Rommy Sanjaya
* @since 0.0.1
* @param <T> number.
* @param <U> named number.
*/
public abstract class NamedNumber<T extends Number, U extends NamedNumber<T, ?>> implements Serializable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is <U> for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for wrap an extended 'NamedNumber' object and store it in the cache.


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();
}

}
44 changes: 44 additions & 0 deletions src/test/java/com/blibli/oss/common/helper/NamedNumberTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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<Integer, HttpStatusCode> {

private static final Map<Integer, HttpStatusCode> 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);
}

}