-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from JarvisCraft/primitive-wrappers
Primitive wrappers
- Loading branch information
Showing
18 changed files
with
1,461 additions
and
5 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
java-commons/src/main/java/ru/progrm_jarvis/javacommons/primitive/Numeric.java
This file contains 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,49 @@ | ||
package ru.progrm_jarvis.javacommons.primitive; | ||
|
||
/** | ||
* Interface analog of {@link Number}. | ||
*/ | ||
public interface Numeric { | ||
|
||
/** | ||
* Returns the value as {@code byte}. | ||
* | ||
* @return value as {@code byte} | ||
*/ | ||
byte byteValue(); | ||
|
||
/** | ||
* Returns the value as {@code short}. | ||
* | ||
* @return value as {@code short} | ||
*/ | ||
short shortValue(); | ||
|
||
/** | ||
* Returns the value as {@code int}. | ||
* | ||
* @return value as {@code int} | ||
*/ | ||
int intValue(); | ||
|
||
/** | ||
* Returns the value as {@code long}. | ||
* | ||
* @return value as {@code long} | ||
*/ | ||
long longValue(); | ||
|
||
/** | ||
* Returns the value as {@code float}. | ||
* | ||
* @return value as {@code float} | ||
*/ | ||
float floatValue(); | ||
|
||
/** | ||
* Returns the value as {@code double}. | ||
* | ||
* @return value as {@code double} | ||
*/ | ||
double doubleValue(); | ||
} |
143 changes: 143 additions & 0 deletions
143
...-commons/src/main/java/ru/progrm_jarvis/javacommons/primitive/wrapper/BooleanWrapper.java
This file contains 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,143 @@ | ||
package ru.progrm_jarvis.javacommons.primitive.wrapper; | ||
|
||
import lombok.*; | ||
import lombok.experimental.Delegate; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* {@link PrimitiveWrapper Primitive wrapper} of {@code boolean}. | ||
*/ | ||
public interface BooleanWrapper extends PrimitiveWrapper<Boolean> { | ||
|
||
/** | ||
* Creates new simple boolean wrapper. | ||
* | ||
* @param value initial value of boolean wrapper | ||
* @return created boolean wrapper | ||
*/ | ||
|
||
static BooleanWrapper create(final boolean value) { | ||
return new BooleanBooleanWrapper(value); | ||
} | ||
|
||
/** | ||
* Creates new simple boolean wrapper with initial value set to {@code 0}. | ||
* | ||
* @return created boolean wrapper | ||
*/ | ||
static BooleanWrapper create() { | ||
return new BooleanBooleanWrapper(false); | ||
} | ||
|
||
/** | ||
* Creates new atomic boolean wrapper. | ||
* | ||
* @param value initial value of boolean wrapper | ||
* @return created boolean wrapper | ||
*/ | ||
static BooleanWrapper createAtomic(final boolean value) { | ||
return new AtomicBooleanWrapper(value); | ||
} | ||
|
||
/** | ||
* Creates new atomic boolean wrapper with initial value set to {@code 0}. | ||
* | ||
* @return created boolean wrapper | ||
*/ | ||
static BooleanWrapper createAtomic() { | ||
return new AtomicBooleanWrapper(); | ||
} | ||
|
||
@Override | ||
default Class<Boolean> getPrimitiveClass() { | ||
return boolean.class; | ||
} | ||
|
||
@Override | ||
default Class<Boolean> getWrapperClass() { | ||
return Boolean.class; | ||
} | ||
|
||
/** | ||
* Gets the value. | ||
* | ||
* @return value | ||
*/ | ||
boolean get(); | ||
|
||
/** | ||
* Sets the value. | ||
* | ||
* @param value value to be set | ||
*/ | ||
void set(boolean value); | ||
|
||
/** | ||
* Sets the value to the one given returning the previous one. | ||
* | ||
* @param newValue value to be set | ||
* @return previous value | ||
*/ | ||
boolean getAndSet(boolean newValue); | ||
|
||
/** | ||
* {@link BooleanWrapper} implementation based on {@code boolean}. | ||
*/ | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(callSuper = false) | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
final class BooleanBooleanWrapper implements BooleanWrapper { | ||
|
||
boolean value; | ||
|
||
@Override | ||
public boolean get() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void set(final boolean value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean getAndSet(final boolean newValue) { | ||
val oldValue = value; | ||
value = newValue; | ||
|
||
return oldValue; | ||
} | ||
} | ||
|
||
/** | ||
* {@link BooleanWrapper} implementation based on {@link AtomicBoolean}. | ||
*/ | ||
@ToString | ||
@EqualsAndHashCode(callSuper = false) | ||
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
final class AtomicBooleanWrapper implements BooleanWrapper { | ||
|
||
@Delegate(types = BooleanWrapper.class, excludes = PrimitiveWrapper.class) | ||
@NonNull AtomicBoolean value; | ||
|
||
/** | ||
* Creates new atomic boolean boolean wrapper. | ||
* | ||
* @param value initial value | ||
*/ | ||
private AtomicBooleanWrapper(final boolean value) { | ||
this.value = new AtomicBoolean(value); | ||
} | ||
|
||
/** | ||
* Creates new atomic boolean boolean wrapper with initial value set to {@code 0}. | ||
*/ | ||
private AtomicBooleanWrapper() { | ||
value = new AtomicBoolean(); | ||
} | ||
} | ||
} |
Oops, something went wrong.