diff --git a/src/main/java/org/springframework/data/redis/connection/FutureResult.java b/src/main/java/org/springframework/data/redis/connection/FutureResult.java new file mode 100644 index 000000000..4f20c00f9 --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/FutureResult.java @@ -0,0 +1,75 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection; + +import org.springframework.core.convert.converter.Converter; + +/** + * The result of an asynchronous operation + * + * @author Jennifer Hickey + * + * @param + * The data type of the object that holds the future result (usually + * of type Future) + */ +abstract public class FutureResult { + + protected T resultHolder; + + @SuppressWarnings("rawtypes") + protected Converter converter; + + public FutureResult(T resultHolder) { + this.resultHolder = resultHolder; + } + + @SuppressWarnings("rawtypes") + public FutureResult(T resultHolder, Converter converter) { + this.resultHolder = resultHolder; + this.converter = converter; + } + + public T getResultHolder() { + return resultHolder; + } + + /** + * Converts the given result if a converter is specified, else returns the + * result + * + * @param result + * The result to convert + * @return The converted result + */ + @SuppressWarnings("unchecked") + public Object convert(Object result) { + if (converter != null) { + return converter.convert(result); + } + return result; + } + + @SuppressWarnings("rawtypes") + public Converter getConverter() { + return converter; + } + + /** + * @return The result of the operation + */ + abstract public Object get(); +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/Converters.java b/src/main/java/org/springframework/data/redis/connection/convert/Converters.java new file mode 100644 index 000000000..147241af0 --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/Converters.java @@ -0,0 +1,64 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.convert; + +import java.util.Properties; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.redis.connection.DataType; + +/** + * Common type converters + * + * @author Jennifer Hickey + * + */ +abstract public class Converters { + + private static final byte[] ONE = new byte[] { '1' }; + private static final byte[] ZERO = new byte[] { '0' }; + private static final Converter STRING_TO_PROPS = new StringToPropertiesConverter(); + private static final Converter LONG_TO_BOOLEAN = new LongToBooleanConverter(); + private static final Converter STRING_TO_DATA_TYPE = new StringToDataTypeConverter(); + + public static Converter stringToProps() { + return STRING_TO_PROPS; + } + + public static Converter longToBoolean() { + return LONG_TO_BOOLEAN; + } + + public static Converter stringToDataType() { + return STRING_TO_DATA_TYPE; + } + + public static Properties toProperties(String source) { + return STRING_TO_PROPS.convert(source); + } + + public static Boolean toBoolean(Long source) { + return LONG_TO_BOOLEAN.convert(source); + } + + public static DataType toDataType(String source) { + return STRING_TO_DATA_TYPE.convert(source); + } + + public static byte[] toBit(Boolean source) { + return (source ? ONE : ZERO); + } +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/IdentityConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/IdentityConverter.java new file mode 100644 index 000000000..e9c287966 --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/IdentityConverter.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.convert; + +import org.springframework.core.convert.converter.Converter; + +/** + * + * Implementation of {@link Converter} that simply returns the value passed in + * + * @author Jennifer Hickey + * + * @param + * The value type + */ +public class IdentityConverter implements Converter { + + public T convert(T source) { + return source; + } +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/ListConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/ListConverter.java new file mode 100644 index 000000000..0d9e27cca --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/ListConverter.java @@ -0,0 +1,43 @@ +package org.springframework.data.redis.connection.convert; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.core.convert.converter.Converter; + +/** + * Converts a List of values of one type to a List of values of another type + * + * @author Jennifer Hickey + * + * @param + * The type of elements in the List to convert + * @param + * The type of elements in the converted List + */ +public class ListConverter implements Converter, List> { + + private Converter itemConverter; + + /** + * + * @param itemConverter + * The {@link Converter} to use for converting individual List + * items + */ + public ListConverter(Converter itemConverter) { + this.itemConverter = itemConverter; + } + + public List convert(List source) { + if (source == null) { + return null; + } + List results = new ArrayList(); + for (S result : source) { + results.add(itemConverter.convert(result)); + } + return results; + } + +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/LongToBooleanConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/LongToBooleanConverter.java new file mode 100644 index 000000000..221f80192 --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/LongToBooleanConverter.java @@ -0,0 +1,32 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.convert; + +import org.springframework.core.convert.converter.Converter; + +/** + * Converts Longs to Booleans + * + * @author Jennifer Hickey + * + */ +public class LongToBooleanConverter implements Converter { + + public Boolean convert(Long result) { + return result != null ? result == 1 : null; + } + +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/MapConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/MapConverter.java new file mode 100644 index 000000000..73ec01377 --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/MapConverter.java @@ -0,0 +1,66 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.convert; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.springframework.core.convert.converter.Converter; + +/** + * Converts a Map of values of one key/value type to a Map of values of another + * type + * + * @author Jennifer Hickey + * + * @param + * The type of keys and values in the Map to convert + * @param + * The type of keys and values in the converted Map + */ +public class MapConverter implements Converter, Map> { + + private Converter itemConverter; + + /** + * + * @param itemConverter + * The {@link Converter} to use for converting individual Map + * keys and values + */ + public MapConverter(Converter itemConverter) { + this.itemConverter = itemConverter; + } + + public Map convert(Map source) { + if (source == null) { + return null; + } + Map results; + if (source instanceof LinkedHashMap) { + results = new LinkedHashMap(); + } else { + results = new HashMap(); + } + for (Map.Entry result : source.entrySet()) { + results.put(itemConverter.convert(result.getKey()), + itemConverter.convert(result.getValue())); + } + return results; + } + +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/SetConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/SetConverter.java new file mode 100644 index 000000000..7a922564b --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/SetConverter.java @@ -0,0 +1,64 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.convert; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.springframework.core.convert.converter.Converter; + +/** + * Converts a Set of values of one type to a Set of values of another type + * + * @author Jennifer Hickey + * + * @param + * The type of elements in the Set to convert + * @param + * The type of elements in the converted Set + */ +public class SetConverter implements Converter, Set> { + + private Converter itemConverter; + + /** + * + * @param itemConverter + * The {@link Converter} to use for converting individual Set + * items + */ + public SetConverter(Converter itemConverter) { + this.itemConverter = itemConverter; + } + + public Set convert(Set source) { + if (source == null) { + return null; + } + Set results; + if (source instanceof LinkedHashSet) { + results = new LinkedHashSet(); + } else { + results = new HashSet(); + } + for (S result : source) { + results.add(itemConverter.convert(result)); + } + return results; + } + +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/StringToDataTypeConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/StringToDataTypeConverter.java new file mode 100644 index 000000000..46d44b293 --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/StringToDataTypeConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.convert; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.redis.connection.DataType; + +/** + * Converts Strings to {@link DataType}s + * + * @author Jennifer Hickey + * + */ +public class StringToDataTypeConverter implements Converter { + + public DataType convert(String source) { + if (source == null) { + return null; + } + return DataType.fromCode(source); + } + +} diff --git a/src/main/java/org/springframework/data/redis/connection/convert/StringToPropertiesConverter.java b/src/main/java/org/springframework/data/redis/connection/convert/StringToPropertiesConverter.java new file mode 100644 index 000000000..16be5493f --- /dev/null +++ b/src/main/java/org/springframework/data/redis/connection/convert/StringToPropertiesConverter.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.convert; + +import java.io.StringReader; +import java.util.Properties; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.redis.RedisSystemException; + +/** + * Converts Strings to {@link Properties} + * + * @author Jennifer Hickey + * + */ +public class StringToPropertiesConverter implements Converter { + + public Properties convert(String source) { + if (source == null) { + return null; + } + Properties info = new Properties(); + StringReader stringReader = new StringReader(source); + try { + info.load(stringReader); + } catch (Exception ex) { + throw new RedisSystemException("Cannot read Redis info", ex); + } finally { + stringReader.close(); + } + return info; + } +}