Skip to content

Commit 0d7bb4a

Browse files
committed
javadoc
1 parent 5ef8581 commit 0d7bb4a

File tree

12 files changed

+301
-133
lines changed

12 files changed

+301
-133
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package org.comroid.api;
2+
3+
import org.comroid.util.Bitmask;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.Collections;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* Helper interface for enum classes that store an integer bitmask.
12+
* <p>
13+
* The default generated bitmasks are dependent on the order of constants.
14+
*
15+
* @param <S> The implementing Enum type
16+
* @see #getValue() for further information
17+
* @see Named Default Enum implementation
18+
*/
19+
public interface BitmaskAttribute<S extends BitmaskAttribute<S>> extends IntegerAttribute, SelfDeclared<S>, Named {
20+
/**
21+
* Computes a default integer value for this bitmask, depending on enum order.
22+
* If implemented by an enum class, this method provides unique default bitmasks for every enum constant.
23+
*
24+
* @return The integer value of this Bitmask constant.
25+
* @see IntegerAttribute#getValue() Returns Enums ordinal value if possible
26+
*/
27+
@Override
28+
@NotNull
29+
default Integer getValue() {
30+
return 1 << IntegerAttribute.super.getValue();
31+
}
32+
33+
/**
34+
* Creates a set of all mask attributes from an integer value and an enum class.
35+
*
36+
* @param mask The integer value to scan
37+
* @param viaEnum The enum to use all constants from.
38+
* @param <T> The enum type.
39+
* @return A set of all Bitmask attributes set in the integer value
40+
*/
41+
static <T extends java.lang.Enum<? extends T> & BitmaskAttribute<T>> Set<T> valueOf(int mask, Class<T> viaEnum) {
42+
if (!viaEnum.isEnum())
43+
throw new IllegalArgumentException("Only enums allowed as parameter 'viaEnum'");
44+
45+
return valueOf(mask, viaEnum.getEnumConstants());
46+
}
47+
48+
49+
/**
50+
* Creates a set of all mask attributes from an integer value and an enum class.
51+
*
52+
* @param mask The integer value to scan
53+
* @param values All possible mask attributes
54+
* @param <T> The enum type.
55+
* @return A set of all Bitmask attributes set in the integer value
56+
*/
57+
static <T extends BitmaskAttribute<T>> Set<T> valueOf(int mask, T[] values) {
58+
HashSet<T> yields = new HashSet<>();
59+
60+
for (T constant : values) {
61+
if (constant.isFlagSet(mask))
62+
yields.add(constant);
63+
}
64+
65+
return Collections.unmodifiableSet(yields);
66+
}
67+
68+
/**
69+
* Creates an integer value containing all provided Bitmask attributes.
70+
*
71+
* @param values All values to combine
72+
* @return The result integer value
73+
*/
74+
static int toMask(BitmaskAttribute<?>[] values) {
75+
int x = 0;
76+
for (BitmaskAttribute<?> each : values)
77+
x = each.apply(x, true);
78+
return x;
79+
}
80+
81+
/**
82+
* Checks whether this Bitmask attribute contains another attribute.
83+
*
84+
* @param other The other attribute.
85+
* @return Whether the other attribute is contained in this attribute
86+
*/
87+
default boolean hasFlag(BitmaskAttribute<S> other) {
88+
return Bitmask.isFlagSet(getValue(), other.getValue());
89+
}
90+
91+
/**
92+
* Checks whether this attribute is set within an integer mask.
93+
*
94+
* @param inMask The mask to check.
95+
* @return Whether this attribute is contained in the mask
96+
*/
97+
default boolean isFlagSet(int inMask) {
98+
return Bitmask.isFlagSet(inMask, getValue());
99+
}
100+
101+
/**
102+
* Applies the {@code newState} of this attribute to the given mask, and returns the result.
103+
*
104+
* @param toMask The mask to apply this attribute to
105+
* @param newState The desired state of this attribute within the mask
106+
* @return The new mask
107+
*/
108+
default int apply(int toMask, boolean newState) {
109+
return Bitmask.modifyFlag(toMask, getValue(), newState);
110+
}
111+
112+
/**
113+
* {@linkplain Object#equals(Object) Equals-implementation} to accept instances of BitmaskAttribute
114+
*
115+
* @param other The attribute to check against.
116+
* @return Whether the attribute values are equal
117+
*/
118+
default boolean equals(BitmaskAttribute<?> other) {
119+
return getValue() == (int) other.getValue();
120+
}
121+
}

src/main/java/org/comroid/api/BitmaskEnum.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/main/java/org/comroid/api/Composer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
import java.util.concurrent.CompletableFuture;
44

5+
/**
6+
* A builder-like interface which returns a {@link CompletableFuture} to complete with the product, instead of the finished object.
7+
*
8+
* @param <T> The type of the result object
9+
*/
510
public interface Composer<T> extends Provider<T> {
11+
/**
12+
* Starts computation for the creation of the object.
13+
*
14+
* @return A future to complete with the final object.
15+
*/
616
CompletableFuture<T> compose();
717

818
@Override

src/main/java/org/comroid/api/IntEnum.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.comroid.api;
2+
3+
import org.comroid.util.StandardValueType;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
/**
7+
* An attribute interface for objects that contains a plain integer value.
8+
*
9+
* @see Named
10+
*/
11+
public interface IntegerAttribute extends Named, ValueBox<Integer> {
12+
/**
13+
* Default implementation. If this is an instance of {@link Enum}, the {@linkplain Enum#ordinal() enum ordinal} is returned.
14+
* Otherwise, an {@link AbstractMethodError} will be thrown.
15+
*
16+
* @return The integer value
17+
* @throws AbstractMethodError If this is not an {@link Enum} instance
18+
*/
19+
@Override
20+
default @NotNull Integer getValue() throws AbstractMethodError {
21+
if (this instanceof Enum)
22+
return ((Enum<?>) this).ordinal();
23+
throw new AbstractMethodError();
24+
}
25+
26+
/**
27+
* Default implementation to obtain the {@link StandardValueType#INTEGER default integer value type} for {@link ValueBox}.
28+
*
29+
* @return {@link StandardValueType#INTEGER}
30+
*/
31+
@Override
32+
default ValueType<? extends Integer> getHeldType() {
33+
return StandardValueType.INTEGER;
34+
}
35+
36+
/**
37+
* Returns a {@link Rewrapper} supplying an instance of the corresponding attribute within the given enum class.
38+
*
39+
* @param value The integer value
40+
* @param viaEnum The enum the check it's attributes
41+
* @param <T> The enum type
42+
* @return A Rewrapper that supplies the result attribute, or an empty rewrapper.
43+
*/
44+
static <T extends java.lang.Enum<? extends T> & IntegerAttribute> Rewrapper<T> valueOf(int value, Class<T> viaEnum) {
45+
if (!viaEnum.isEnum())
46+
throw new IllegalArgumentException("Only enums allowed as parameter 'viaEnum'");
47+
48+
return valueOf(value, viaEnum.getEnumConstants());
49+
}
50+
51+
52+
/**
53+
* Returns a {@link Rewrapper} supplying an instance of the corresponding attribute within the given enum class.
54+
*
55+
* @param value The integer value
56+
* @param constants All possible values
57+
* @param <T> The enum type
58+
* @return A Rewrapper that supplies the result attribute, or an empty rewrapper.
59+
*/
60+
static <T extends IntegerAttribute> Rewrapper<T> valueOf(int value, final T[] constants) {
61+
for (T it : constants)
62+
if (it.getValue() == value)
63+
return () -> it;
64+
return Rewrapper.empty();
65+
}
66+
67+
/**
68+
* An equals-implementation to accept int values.
69+
*
70+
* @param value The value to check
71+
* @return Whether this objects value and the other value are equal.
72+
*/
73+
default boolean equals(int value) {
74+
return getValue() == value;
75+
}
76+
}
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
11
package org.comroid.api;
22

33
public interface MutableState {
4+
/**
5+
* Checks whether this container object is currently allowed to change its value.
6+
*
7+
* @return whether this container object is currently allowed to change its value
8+
*/
49
boolean isMutable();
510

11+
/**
12+
* Checks whether this container object is currently prohibited to change its value.
13+
*
14+
* @return whether this container object is currently prohibited to change its value.
15+
*/
616
default boolean isImmutable() {
717
return !isMutable();
818
}
919

20+
/**
21+
* Attempts to change the mutability state of this container object.
22+
* <p>
23+
* If the return value is {@code true}, the new {@code state} could be applied.
24+
* Returns {@code false} if otherwise.
25+
*
26+
* @param state The new mutability state
27+
* @return Whether the new mutability state could be applied
28+
*/
1029
boolean setMutable(boolean state);
1130

12-
default boolean setMutable() {
13-
return setMutable(true);
31+
/**
32+
* Attempts to allow mutation of this container object.
33+
*
34+
* @throws UnsupportedOperationException if this object could not be made mutable. May be caused by object already being mutable.
35+
*/
36+
default void setMutable() throws UnsupportedOperationException {
37+
if (!setMutable(true))
38+
throw new UnsupportedOperationException("Could not make " + this + " mutable");
1439
}
1540

16-
default boolean setImmutable() {
17-
return setMutable(false);
41+
/**
42+
* Attempts to disallow mutation of this container object.
43+
*
44+
* @throws UnsupportedOperationException if this object could not be made immutable. May be caused by object already being mutable.
45+
*/
46+
default void setImmutable() {
47+
if (!setMutable(false))
48+
throw new UnsupportedOperationException("Could not make " + this + " immutable");
1849
}
1950
}

0 commit comments

Comments
 (0)