Skip to content

Commit

Permalink
feet: added StringValue.Horizontal, and StringValue.Vertical as a par…
Browse files Browse the repository at this point in the history
…ameter orientation in (@StringValue)
  • Loading branch information
MouamleH committed Jun 1, 2019
1 parent 7fe44e8 commit 86ffeb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface StringValue {

int Horizontal = 0;
int Vertical = 1;

String key();

String[] values();

int orientation() default Horizontal;

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package mouamle.generator.annotation.handlers;

import mouamle.registry.AnnotationHandler;
import mouamle.generator.classes.ButtonHolder;
import mouamle.registry.AnnotationHandler;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StringValueHandler implements AnnotationHandler<StringValue> {
Expand All @@ -16,6 +17,29 @@ public List<List<ButtonHolder>> generate(StringValue stringValue, Object o, Fiel
List<List<ButtonHolder>> part = new ArrayList<>();
addValueHeader(valueKey, part);

switch (stringValue.orientation()) {
case StringValue.Horizontal:
generateHorizontal(stringValue, field, valueKey, part);
break;
case StringValue.Vertical:
generateVertical(stringValue, field, valueKey, part);
break;
default:
throw new IllegalArgumentException("The orientation must be StringValue.Horizontal or StringValue.Vertical");
}

return part;
}

private void generateVertical(StringValue stringValue, Field field, String valueKey, List<List<ButtonHolder>> part) {
String[] values = stringValue.values();
for (String value : values) {
String data = String.format("StringValue;%s;%s;%s", field.getName(), valueKey, value);
part.add(Collections.singletonList(new ButtonHolder(value, data)));
}
}

private void generateHorizontal(StringValue stringValue, Field field, String valueKey, List<List<ButtonHolder>> part) {
List<ButtonHolder> row = new ArrayList<>();

String[] values = stringValue.values();
Expand All @@ -25,7 +49,6 @@ public List<List<ButtonHolder>> generate(StringValue stringValue, Object o, Fiel
}

part.add(row);
return part;
}

}
2 changes: 1 addition & 1 deletion src/test/java/mouamle/keyboard/bot/model/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Data {
@IntValue(key = "Age")
private int age;

@StringValue(key = "Color", values = {"r", "g", "b", "a"})
@StringValue(key = "Color", values = {"r", "g", "b", "a"}, orientation = StringValue.Vertical)
private String color;

@IntValue(key = "Size")
Expand Down

0 comments on commit 86ffeb6

Please sign in to comment.