-
Notifications
You must be signed in to change notification settings - Fork 3
Simple Fields
Wiehann Matthysen edited this page Mar 24, 2012
·
4 revisions
Strawberry supports the following simple field-types:
- char([]) (and Character([]))
- String
- byte([]) (and Byte([]))
- boolean (and Boolean)
- short (and Short)
- int (and Integer)
- long (and Long)
- BigInteger
- float (and Float)
- double (and Double)
- BigDecimal
Auto-conversion of the value in the Redis database to the target field-type will be attempted by strawberry. If no conversion is possible (i.e. the value in the Redis database is invalid) an appropriate ConversionException will be thrown containing details of the key and value-pair that caused the exception.
To illustrate this functionality, we are going to populate our Redis database with a couple of values:
redis 127.0.0.1:6379> set test:char "B"
OK
redis 127.0.0.1:6379> set test:string "test value"
OK
redis 127.0.0.1:6379> set test:number "12"
OK
redis 127.0.0.1:6379> set test:decimal "0.123456"
OK
redis 127.0.0.1:6379> set test:boolean "True"
OK
redis 127.0.0.1:6379> exit
Now, assuming we have a class called ConfigStore to serve as container for all of these values:
import java.math.BigDecimal;
import java.math.BigInteger;
import com.github.strawberry.guice.Redis;
public class ConfigStore {
@Redis("test:char")
private char testChar;
@Redis("test:char")
private Character testCharacter;
@Redis("test:string")
private char[] testChars;
@Redis("test:string")
private Character[] testCharacters;
@Redis("test:string")
private String testString;
@Redis("test:number")
private byte testpByte;
@Redis("test:number")
private Byte testByte;
@Redis("test:string")
private byte[] testpBytes;
@Redis("test:string")
private Byte[] testBytes;
@Redis("test:boolean")
private boolean testpBoolean;
@Redis("test:boolean")
private Boolean testBoolean;
@Redis("test:number")
private short testpShort;
@Redis("test:number")
private Short testShort;
@Redis("test:number")
private int testInt;
@Redis("test:number")
private Integer testInteger;
@Redis("test:number")
private long testpLong;
@Redis("test:number")
private Long testLong;
@Redis("test:number")
private BigInteger testBigInteger;
@Redis("test:decimal")
private float testpFloat;
@Redis("test:decimal")
private Float testFloat;
@Redis("test:decimal")
private double testpDouble;
@Redis("test:decimal")
private Double testDouble;
@Redis("test:decimal")
private BigDecimal testBigDecimal;
public char getChar() {
return this.testChar;
}
public Character getCharacter() {
return this.testCharacter;
}
public char[] getChars() {
return this.testChars;
}
public Character[] getCharacters() {
return this.testCharacters;
}
public String getString() {
return this.testString;
}
public byte getPByte() {
return this.testpByte;
}
public Byte getByte() {
return this.testByte;
}
public byte[] getPBytes() {
return this.testpBytes;
}
public Byte[] getBytes() {
return this.testBytes;
}
public boolean getPBoolean() {
return this.testpBoolean;
}
public Boolean getBoolean() {
return this.testBoolean;
}
public short getPShort() {
return this.testpShort;
}
public Short getShort() {
return this.testShort;
}
public int getInt() {
return this.testInt;
}
public Integer getInteger() {
return this.testInteger;
}
public long getPLong() {
return this.testpLong;
}
public Long getLong() {
return this.testLong;
}
public BigInteger getBigInteger() {
return this.testBigInteger;
}
public float getPFloat() {
return this.testpFloat;
}
public Float getFloat() {
return this.testFloat;
}
public double getPDouble() {
return this.testpDouble;
}
public Double getDouble() {
return this.testDouble;
}
public BigDecimal getBigDecimal() {
return this.testBigDecimal;
}
}
Then, the following unit-test should pass:
import org.junit.Test;
import com.github.strawberry.guice.RedisModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import redis.clients.jedis.JedisPool;
import static org.apache.commons.lang3.ArrayUtils.toPrimitive;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
public class TestConfigStore {
private final JedisPool pool = new JedisPool("localhost", 6379);
@Test
public void testConfigstore() {
Injector injector = Guice.createInjector(new RedisModule(this.pool));
ConfigStore store = injector.getInstance(ConfigStore.class);
assertThat(store.getChar(), is('B'));
assertThat(store.getCharacter(), is('B'));
assertThat(store.getChars(), is(equalTo("test value".toCharArray())));
assertThat(toPrimitive(store.getCharacters()), is(equalTo("test value".toCharArray())));
assertThat(store.getString(), is(equalTo("test value")));
assertThat(store.getPByte(), is((byte)12));
assertThat(store.getByte(), is((byte)(12)));
assertThat(store.getPBytes(), is(equalTo("test value".getBytes())));
assertThat(toPrimitive(store.getBytes()), is(equalTo("test value".getBytes())));
assertThat(store.getPBoolean(), is(true));
assertThat(store.getBoolean(), is(true));
assertThat(store.getPShort(), is((short)12));
assertThat(store.getShort(), is((short)12));
assertThat(store.getInt(), is(12));
assertThat(store.getInteger(), is(12));
assertThat(store.getPLong(), is(12L));
assertThat(store.getLong(), is(12L));
assertThat(store.getBigInteger().intValue(), is(12));
assertThat(store.getPFloat(), is(0.123456f));
assertThat(store.getFloat(), is(0.123456f));
assertThat(store.getPDouble(), is(0.123456));
assertThat(store.getDouble(), is(0.123456));
assertThat(store.getBigDecimal().doubleValue(), is(0.123456));
}
}