Skip to content

Commit

Permalink
Merge pull request #83 from JarvisCraft/primitive-wrappers
Browse files Browse the repository at this point in the history
Primitive wrappers
  • Loading branch information
JarvisCraft committed Apr 25, 2020
2 parents 9533f86 + a4faea0 commit 2e52755
Show file tree
Hide file tree
Showing 18 changed files with 1,461 additions and 5 deletions.
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();
}
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();
}
}
}
Loading

0 comments on commit 2e52755

Please sign in to comment.