-
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.
separated mutable and immutable from SelectionStrategy
- Loading branch information
1 parent
ba92bce
commit ec0d127
Showing
10 changed files
with
275 additions
and
141 deletions.
There are no files selected for viewing
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
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
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
61 changes: 61 additions & 0 deletions
61
jargyle-server/src/main/java/com/github/jh3nd3rs0n/jargyle/server/SelectionStrategySpec.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,61 @@ | ||
package com.github.jh3nd3rs0n.jargyle.server; | ||
|
||
import java.util.Objects; | ||
|
||
public abstract class SelectionStrategySpec { | ||
|
||
private final String name; | ||
|
||
SelectionStrategySpec(final String n) { | ||
Objects.requireNonNull(n); | ||
this.name = n; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (this.getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
SelectionStrategySpec other = (SelectionStrategySpec) obj; | ||
if (this.name == null) { | ||
if (other.name != null) { | ||
return false; | ||
} | ||
} else if (!this.name.equals(other.name)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public final String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((this.name == null) ? | ||
0 : this.name.hashCode()); | ||
return result; | ||
} | ||
|
||
public abstract SelectionStrategy newSelectionStrategy(); | ||
|
||
@Override | ||
public final String toString() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append(this.getClass().getSimpleName()) | ||
.append(" [name=") | ||
.append(this.name) | ||
.append("]"); | ||
return builder.toString(); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...er/src/main/java/com/github/jh3nd3rs0n/jargyle/server/SelectionStrategySpecConstants.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,74 @@ | ||
package com.github.jh3nd3rs0n.jargyle.server; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import com.github.jh3nd3rs0n.jargyle.internal.annotation.HelpText; | ||
import com.github.jh3nd3rs0n.jargyle.server.internal.selectionstrategy.impl.CyclicalSelectionStrategy; | ||
import com.github.jh3nd3rs0n.jargyle.server.internal.selectionstrategy.impl.RandomSelectionStrategy; | ||
|
||
public final class SelectionStrategySpecConstants { | ||
|
||
private static final SelectionStrategySpecs SELECTION_STRATEGY_SPECS = | ||
new SelectionStrategySpecs(); | ||
|
||
@HelpText( | ||
doc = "Select the next in the cycle", | ||
usage = "CYCLICAL" | ||
) | ||
public static final SelectionStrategySpec CYCLICAL = SELECTION_STRATEGY_SPECS.addThenGet(new SelectionStrategySpec( | ||
"CYCLICAL") { | ||
|
||
@Override | ||
public SelectionStrategy newSelectionStrategy() { | ||
return new CyclicalSelectionStrategy(this); | ||
} | ||
|
||
}); | ||
|
||
@HelpText( | ||
doc = "Select the next at random", | ||
usage = "RANDOM" | ||
) | ||
public static final SelectionStrategySpec RANDOM = SELECTION_STRATEGY_SPECS.addThenGet(new SelectionStrategySpec( | ||
"RANDOM") { | ||
|
||
@Override | ||
public SelectionStrategy newSelectionStrategy() { | ||
return new RandomSelectionStrategy(this); | ||
} | ||
|
||
}); | ||
|
||
private static final List<SelectionStrategySpec> VALUES = | ||
SELECTION_STRATEGY_SPECS.toList(); | ||
|
||
private static final Map<String, SelectionStrategySpec> VALUES_MAP = | ||
SELECTION_STRATEGY_SPECS.toMap(); | ||
|
||
public static SelectionStrategySpec valueOfName(final String name) { | ||
if (VALUES_MAP.containsKey(name)) { | ||
return VALUES_MAP.get(name); | ||
} | ||
String str = VALUES.stream() | ||
.map(SelectionStrategySpec::getName) | ||
.collect(Collectors.joining(", ")); | ||
throw new IllegalArgumentException(String.format( | ||
"expected selection strategy spec must be one of the " | ||
+ "following values: %s. actual value is %s", | ||
str, | ||
name)); | ||
} | ||
|
||
public static List<SelectionStrategySpec> values() { | ||
return VALUES; | ||
} | ||
|
||
public static Map<String, SelectionStrategySpec> valuesMap() { | ||
return VALUES_MAP; | ||
} | ||
|
||
private SelectionStrategySpecConstants() { } | ||
|
||
} |
Oops, something went wrong.