-
Notifications
You must be signed in to change notification settings - Fork 0
Add NamedNumber utility #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ardikars
wants to merge
2
commits into
bliblidotcom:master
Choose a base branch
from
ardikars:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
src/main/java/com/blibli/oss/common/helper/util/NamedNumber.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
| 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
44
src/test/java/com/blibli/oss/common/helper/NamedNumberTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
|
|
||
| } |
47 changes: 47 additions & 0 deletions
47
src/test/java/com/blibli/oss/common/helper/config/HttpStatusCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is <U> for?
There was a problem hiding this comment.
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.