preferences added
This commit is contained in:
34
src/main/java/org/solovyev/common/BooleanMapper.java
Normal file
34
src/main/java/org/solovyev/common/BooleanMapper.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.common;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.math.ValueOfFormatter;
|
||||
import org.solovyev.common.utils.Formatter;
|
||||
import org.solovyev.common.utils.Mapper;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/11
|
||||
* Time: 11:27 PM
|
||||
*/
|
||||
public class BooleanMapper implements Mapper<Boolean>{
|
||||
|
||||
@NotNull
|
||||
private final Formatter<Boolean> formatter = new ValueOfFormatter<Boolean>();
|
||||
|
||||
@Override
|
||||
public String formatValue(@Nullable Boolean value) throws IllegalArgumentException {
|
||||
return formatter.formatValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean parseValue(@Nullable String value) throws IllegalArgumentException {
|
||||
return value == null ? null : Boolean.valueOf(value);
|
||||
}
|
||||
}
|
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.common;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.math.ValueOfFormatter;
|
||||
import org.solovyev.common.utils.Formatter;
|
||||
import org.solovyev.common.utils.Parser;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/21/11
|
||||
* Time: 12:06 AM
|
||||
*/
|
||||
public class FloatIntervalMapper extends AbstractIntervalMapper<Float> {
|
||||
@NotNull
|
||||
@Override
|
||||
protected Formatter<Float> getFormatter() {
|
||||
return new ValueOfFormatter<Float>();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Parser<Float> getParser() {
|
||||
return new Parser<Float>() {
|
||||
@Override
|
||||
public Float parseValue(@Nullable String s) throws IllegalArgumentException {
|
||||
return Float.valueOf(s);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
39
src/main/java/org/solovyev/common/GenericIntervalMapper.java
Normal file
39
src/main/java/org/solovyev/common/GenericIntervalMapper.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.common;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.common.utils.Formatter;
|
||||
import org.solovyev.common.utils.Mapper;
|
||||
import org.solovyev.common.utils.Parser;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/11
|
||||
* Time: 10:45 PM
|
||||
*/
|
||||
public class GenericIntervalMapper<T> extends AbstractIntervalMapper<T> {
|
||||
|
||||
@NotNull
|
||||
private final Mapper<T> mapper;
|
||||
|
||||
public GenericIntervalMapper(@NotNull Mapper<T> mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Formatter<T> getFormatter() {
|
||||
return mapper;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Parser<T> getParser() {
|
||||
return mapper;
|
||||
}
|
||||
}
|
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.common;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.math.ValueOfFormatter;
|
||||
import org.solovyev.common.utils.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/11
|
||||
* Time: 11:56 PM
|
||||
*/
|
||||
public class IntegerIntervalMapper extends AbstractIntervalMapper<Integer> {
|
||||
|
||||
@NotNull
|
||||
protected Formatter<Integer> getFormatter() {
|
||||
return new ValueOfFormatter<Integer>();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected Parser<Integer> getParser() {
|
||||
return new Parser<Integer>() {
|
||||
@Override
|
||||
public Integer parseValue(@Nullable String s) throws IllegalArgumentException {
|
||||
return Integer.valueOf(s);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
21
src/main/java/org/solovyev/common/NumberIntervalMapper.java
Normal file
21
src/main/java/org/solovyev/common/NumberIntervalMapper.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.common;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/11
|
||||
* Time: 11:56 PM
|
||||
*/
|
||||
public class NumberIntervalMapper<T extends Number> extends GenericIntervalMapper<T> {
|
||||
|
||||
public NumberIntervalMapper(@NotNull Class<T> clazz) {
|
||||
super(new NumberMapper<T>(clazz));
|
||||
}
|
||||
}
|
42
src/main/java/org/solovyev/common/NumberMapper.java
Normal file
42
src/main/java/org/solovyev/common/NumberMapper.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.common;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.math.ValueOfFormatter;
|
||||
import org.solovyev.common.utils.Formatter;
|
||||
import org.solovyev.common.utils.Mapper;
|
||||
import org.solovyev.common.utils.Parser;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/11
|
||||
* Time: 11:10 PM
|
||||
*/
|
||||
public class NumberMapper<T extends Number> implements Mapper<T>{
|
||||
|
||||
@NotNull
|
||||
private final Formatter<T> formatter = new ValueOfFormatter<T>();
|
||||
|
||||
@NotNull
|
||||
private final Parser<T> parser;
|
||||
|
||||
public NumberMapper(@NotNull Class<T> clazz) {
|
||||
this.parser = new NumberParser<T>(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(@Nullable T value) throws IllegalArgumentException {
|
||||
return formatter.formatValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T parseValue(@Nullable String value) throws IllegalArgumentException {
|
||||
return this.parser.parseValue(value);
|
||||
}
|
||||
}
|
45
src/main/java/org/solovyev/common/NumberParser.java
Normal file
45
src/main/java/org/solovyev/common/NumberParser.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.common;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.common.utils.Parser;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/11
|
||||
* Time: 11:07 PM
|
||||
*/
|
||||
public class NumberParser<T extends Number> implements Parser<T> {
|
||||
|
||||
@NotNull
|
||||
private final Class<T> clazz;
|
||||
|
||||
public NumberParser(@NotNull Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T parseValue(@Nullable String value) throws IllegalArgumentException {
|
||||
T result;
|
||||
|
||||
if (value != null) {
|
||||
if (this.clazz.equals(Integer.class)) {
|
||||
result = (T) Integer.valueOf(value);
|
||||
} else if (this.clazz.equals(Float.class)) {
|
||||
result = (T) Float.valueOf(value);
|
||||
} else {
|
||||
throw new UnsupportedOperationException(this.clazz + " is not supported!");
|
||||
}
|
||||
} else {
|
||||
result = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -16,8 +16,19 @@ import org.solovyev.common.utils.Formatter;
|
||||
*/
|
||||
public class ValueOfFormatter<T> implements Formatter<T>{
|
||||
|
||||
private final boolean processNulls;
|
||||
|
||||
public ValueOfFormatter() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public ValueOfFormatter(boolean processNulls) {
|
||||
this.processNulls = processNulls;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String formatValue(@Nullable T t) throws IllegalArgumentException {
|
||||
return String.valueOf(t);
|
||||
return t == null ? (processNulls ? String.valueOf(t) : null) : String.valueOf(t);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user