Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit 3afc9c3

Browse files
committed
Created Cycleable interface
1 parent 1dc2239 commit 3afc9c3

File tree

5 files changed

+108
-29
lines changed

5 files changed

+108
-29
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package clientapi.util.interfaces;
2+
3+
/**
4+
* @author Brady
5+
* @since 4/12/2018 6:53 PM
6+
*/
7+
public interface Cycleable<T> {
8+
9+
/**
10+
* @return The current selected element
11+
*/
12+
T current();
13+
14+
/**
15+
* Sets the current element to the one succeeding it.
16+
*
17+
* @return The next element
18+
*/
19+
T next();
20+
21+
/**
22+
* Sets the current element to the one preceding it.
23+
*
24+
* @return The next element
25+
*/
26+
T last();
27+
28+
/**
29+
* Peeks at the element succeeding the current one
30+
* without setting the current one to it.
31+
*
32+
* @return The next element
33+
*/
34+
T peekNext();
35+
36+
/**
37+
* Peeks at the element preceding the current one
38+
* without setting the current one to it.
39+
*
40+
* @return The next element
41+
*/
42+
T peekLast();
43+
44+
/**
45+
* @return All of the elements in this {@code Cycleable};
46+
*/
47+
T[] getElements();
48+
}

src/main/java/clientapi/value/type/EnumType.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package clientapi.value.type;
1818

19+
import clientapi.util.interfaces.Cycleable;
1920
import clientapi.value.Value;
2021
import clientapi.value.annotation.EnumValue;
2122
import org.apache.commons.lang3.ArrayUtils;
@@ -30,7 +31,7 @@
3031
* @author Brady
3132
* @since 12/1/2017 7:12 PM
3233
*/
33-
public final class EnumType<T extends Enum<?>> extends Value<T> {
34+
public final class EnumType<T extends Enum<?>> extends Value<T> implements Cycleable<T> {
3435

3536
private final T[] values;
3637

@@ -39,27 +40,43 @@ public EnumType(String name, String parent, String id, String description, Objec
3940
this.values = enumClass.getEnumConstants();
4041
}
4142

42-
/**
43-
* Sets value to the next one in the set
44-
*/
45-
public final void next() {
43+
@Override
44+
public final T current() {
45+
return this.getValue();
46+
}
47+
48+
@Override
49+
public final T next() {
50+
T value = peekNext();
51+
this.setValue(value);
52+
return value;
53+
}
54+
55+
@Override
56+
public final T last() {
57+
T value = peekLast();
58+
this.setValue(value);
59+
return value;
60+
}
61+
62+
@Override
63+
public final T peekNext() {
4664
int index = ArrayUtils.indexOf(values, getValue());
4765
if (++index >= values.length)
4866
index = 0;
49-
this.setValue(values[index]);
67+
return values[index];
5068
}
5169

52-
/**
53-
* Sets value to the last one in the set
54-
*/
55-
public final void last() {
70+
@Override
71+
public final T peekLast() {
5672
int index = ArrayUtils.indexOf(values, getValue());
5773
if (--index < 0)
5874
index = values.length - 1;
59-
this.setValue(values[index]);
75+
return values[index];
6076
}
6177

62-
public final T[] getMultiValues() {
78+
@Override
79+
public final T[] getElements() {
6380
return this.values;
6481
}
6582
}

src/main/java/clientapi/value/type/MultiType.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package clientapi.value.type;
1818

1919
import clientapi.util.ClientAPIUtils;
20+
import clientapi.util.interfaces.Cycleable;
2021
import clientapi.value.Value;
2122
import org.apache.commons.lang3.ArrayUtils;
2223

@@ -28,7 +29,7 @@
2829
* @author Brady
2930
* @since 2/24/2017 12:00 PM
3031
*/
31-
public final class MultiType extends Value<String> {
32+
public final class MultiType extends Value<String> implements Cycleable<String> {
3233

3334
/**
3435
* Different values
@@ -46,30 +47,43 @@ public final void setValue(String value) {
4647
super.setValue(ClientAPIUtils.objectFrom(value, values));
4748
}
4849

49-
/**
50-
* Sets value to the next one in the set
51-
*/
52-
public final void next() {
50+
@Override
51+
public final String current() {
52+
return this.getValue();
53+
}
54+
55+
@Override
56+
public final String next() {
57+
String value = peekNext();
58+
this.setValue(value);
59+
return value;
60+
}
61+
62+
@Override
63+
public final String last() {
64+
String value = peekLast();
65+
this.setValue(value);
66+
return value;
67+
}
68+
69+
@Override
70+
public final String peekNext() {
5371
int index = ArrayUtils.indexOf(values, getValue());
5472
if (++index >= values.length)
5573
index = 0;
56-
this.setValue(values[index]);
74+
return values[index];
5775
}
5876

59-
/**
60-
* Sets value to the last one in the set
61-
*/
62-
public final void last() {
77+
@Override
78+
public final String peekLast() {
6379
int index = ArrayUtils.indexOf(values, getValue());
6480
if (--index < 0)
6581
index = values.length - 1;
66-
this.setValue(values[index]);
82+
return values[index];
6783
}
6884

69-
/**
70-
* @return All possible values for this MultiType
71-
*/
72-
public final String[] getMultiValues() {
85+
@Override
86+
public final String[] getElements() {
7387
return this.values;
7488
}
7589
}

src/main/java/clientapi/value/type/resolve/impl/EnumTypeResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final EnumType resolve(Object parent, Field field) {
3939

4040
EnumType type = new EnumType(label.name(), label.parent(), label.id(), label.description(), parent, field, enumType);
4141
if (type.getValue() == null)
42-
type.setValue(type.getMultiValues()[0]);
42+
type.setValue(type.getElements()[0]);
4343

4444
return type;
4545
}

src/main/java/clientapi/value/type/resolve/impl/MultiTypeResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final MultiType resolve(Object parent, Field field) {
3838

3939
MultiType type = new MultiType(label.name(), label.parent(), label.id(), label.description(), parent, field, multi.value());
4040
if (type.getValue() == null)
41-
type.setValue(type.getMultiValues()[0]);
41+
type.setValue(type.getElements()[0]);
4242

4343
return type;
4444
}

0 commit comments

Comments
 (0)