Skip to content

Commit

Permalink
Merge pull request #1 from mrsaraira/enum-containers
Browse files Browse the repository at this point in the history
2.0.0
+ support enum constant containers
  • Loading branch information
mrsaraira authored Oct 19, 2023
2 parents dc6276e + 58beebd commit edbb952
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 141 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
compileOnly "org.projectlombok:lombok:$LIB_LOMBOK_VERSION"
annotationProcessor "org.projectlombok:lombok:$LIB_LOMBOK_VERSION"

testCompileOnly "org.projectlombok:lombok:$LIB_LOMBOK_VERSION"
testAnnotationProcessor "org.projectlombok:lombok:$LIB_LOMBOK_VERSION"
testImplementation platform("org.junit:junit-bom:$LIB_JUNIT_BOM")
testImplementation 'org.junit.jupiter:junit-jupiter'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.mrsaraira.constants.containers.AbstractConstantContainer;

import java.util.Collection;
import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -19,20 +18,12 @@
*/
public interface ConstantContainer<T> {

/**
* Get constant key by value.
*
* @param value contained key value
* @return constant optional
*/
Optional<Constant<T>> getKey(T value);

/**
* Get constant keys.
*
* @return constant keys
*/
Collection<Constant<T>> getKeys();
Collection<Constant<T>> getAllKeys();

/**
* Get all values of the container.
Expand Down
328 changes: 251 additions & 77 deletions src/main/java/io/github/mrsaraira/constants/Constants.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.mrsaraira.constants;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Enumeration constant container that stores a {@link Constant}.
* The constants can be operated using utility class {@link Constants} or custom logic.
* <p>
* {@inheritDoc}
*
* @param <L> relation constant key value type
* @param <E> (self reference) of Enum which implements this {@link EnumConstantContainer}
* @author Takhsin Saraira
*/
public interface EnumConstantContainer<L, E extends Enum<E> & EnumConstantContainer<L, E>> extends ConstantContainer<L> {

/**
* Get enumeration constant.
*
* @return enumeration constant
*/
Constant<L> getConstant();

@Override
default Collection<Constant<L>> getAllKeys() {
return Arrays.stream(values()).map(EnumConstantContainer::getConstant).collect(Collectors.toUnmodifiableSet());
}

@Override
default Set<L> getAllValues() {
return getAllKeys().stream()
.map(Constant::getValue)
.collect(Collectors.toUnmodifiableSet());
}

@SuppressWarnings("unchecked")
private E[] values() {
return Constants.Inner.getEnumValues((Class<E>) getClass());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.mrsaraira.constants;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* Enumeration relation constant container that stores a {@link RelationConstant}.
* The relation constants can be operated using utility class {@link Constants} or custom logic.
* <p>
* {@inheritDoc}
*
* @param <L> relation constant key value type
* @param <R>relation constant relation values type
* @param <E> (self reference) of Enum which implements this {@link EnumRelationConstantContainer}
* @author Takhsin Saraira
*/
public interface EnumRelationConstantContainer<L, R, E extends Enum<E> & EnumRelationConstantContainer<L, R, E>> extends EnumConstantContainer<L, E>, RelationConstantContainer<L, R> {

/**
* Get enumeration relation constant.
*
* @return enum relation constant
*/
RelationConstant<L, R> getConstant();

/**
* Returns relation values of the enumeration relation constant.
*
* @return collection of relation values of the constant.
*/
default Collection<R> getRelationValues() {
return Constants.getValues(getConstant().getRelations());
}

@Override
default Collection<RelationConstant<L, R>> getAllRelations() {
return Arrays.stream(values()).map(EnumRelationConstantContainer::getConstant).collect(Collectors.toUnmodifiableSet());
}

@Override
default List<Collection<R>> getAllRelationsValues() {
return getAllRelations().stream()
.map(RelationConstant::getRelations)
.map(Constants::getValues)
.collect(Collectors.toUnmodifiableList());
}

@SuppressWarnings("unchecked")
private E[] values() {
return Constants.Inner.getEnumValues((Class<E>) getClass());
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.mrsaraira.constants;

import java.util.Collection;

/**
* A constant that contains a key and its relations.
*
Expand All @@ -26,6 +24,6 @@ public interface RelationConstant<L, R> extends Constant<L> {
*
* @return constant relations
*/
Collection<Constant<R>> getRelations();
Constant<R>[] getRelations();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
* A container that stores constants and their relations.
Expand All @@ -21,22 +20,14 @@
public interface RelationConstantContainer<L, R> extends ConstantContainer<L> {

/**
* Get relation constant by key value.
* Get all relation constants of the container.
*
* @param keyValue key value
* @return relation constant
* @return collection of relation constants of the container
*/
Optional<RelationConstant<L, R>> getRelation(L keyValue);
Collection<RelationConstant<L, R>> getAllRelations();

/**
* Get all relations of the container.
*
* @return relation constants of the container
*/
Collection<RelationConstant<L, R>> getRelations();

/**
* Get all relation values of the container.
* Get all relation values of the relation constants of the container.
*
* @return list of relation values collections of the container
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.github.mrsaraira.constants;

import lombok.Value;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.Collection;

Expand All @@ -13,15 +16,24 @@
* @param <R> relations type
* @author Takhsin Saraira
*/
@Value
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
class RelationConstantImpl<L, R> implements RelationConstant<L, R> {

Constant<L> key;
Collection<Constant<R>> relations;
@Getter
private final Constant<L> key;
private final Collection<Constant<R>> relations;

@Override
public L getValue() {
return getKey().getValue();
}

@Override
@SuppressWarnings("unchecked")
public Constant<R>[] getRelations() {
return Constants.toArray(relations, new Constant[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ protected AbstractConstantContainer() {
protected abstract List<Constant<T>> initialConstants();

@Override
public final Optional<Constant<T>> getKey(T value) {
return constants.stream().filter(constant -> Objects.equals(constant.getValue(), value)).findFirst();
}

@Override
public final Collection<Constant<T>> getKeys() {
public final Collection<Constant<T>> getAllKeys() {
return constants;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.mrsaraira.constants.containers;

import io.github.mrsaraira.constants.Constant;
import io.github.mrsaraira.constants.Constants;
import io.github.mrsaraira.constants.RelationConstant;
import io.github.mrsaraira.constants.RelationConstantContainer;

Expand Down Expand Up @@ -49,22 +48,12 @@ protected AbstractRelationConstantContainer() {
protected abstract List<RelationConstant<L, R>> initialConstants();

@Override
public final Optional<Constant<L>> getKey(L value) {
return getKeys().stream().filter(constant -> Objects.equals(constant.getValue(), value)).findFirst();
}

@Override
public final Collection<Constant<L>> getKeys() {
public final Collection<Constant<L>> getAllKeys() {
return constantsMap.keySet();
}

@Override
public final Optional<RelationConstant<L, R>> getRelation(L keyValue) {
return Optional.ofNullable(constantsMap.get(Constants.of(keyValue)));
}

@Override
public final Collection<RelationConstant<L, R>> getRelations() {
public final Collection<RelationConstant<L, R>> getAllRelations() {
return constantsMap.values();
}

Expand Down
Loading

0 comments on commit edbb952

Please sign in to comment.