Add FutureResult and common type converters for pipelined ops
DATAREDIS-200
This commit is contained in:
@@ -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 <T>
|
||||
* The data type of the object that holds the future result (usually
|
||||
* of type Future)
|
||||
*/
|
||||
abstract public class FutureResult<T> {
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -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, Properties> STRING_TO_PROPS = new StringToPropertiesConverter();
|
||||
private static final Converter<Long, Boolean> LONG_TO_BOOLEAN = new LongToBooleanConverter();
|
||||
private static final Converter<String, DataType> STRING_TO_DATA_TYPE = new StringToDataTypeConverter();
|
||||
|
||||
public static Converter<String, Properties> stringToProps() {
|
||||
return STRING_TO_PROPS;
|
||||
}
|
||||
|
||||
public static Converter<Long, Boolean> longToBoolean() {
|
||||
return LONG_TO_BOOLEAN;
|
||||
}
|
||||
|
||||
public static Converter<String, DataType> 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);
|
||||
}
|
||||
}
|
||||
@@ -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 <T>
|
||||
* The value type
|
||||
*/
|
||||
public class IdentityConverter<T> implements Converter<T, T> {
|
||||
|
||||
public T convert(T source) {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -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 <S>
|
||||
* The type of elements in the List to convert
|
||||
* @param <T>
|
||||
* The type of elements in the converted List
|
||||
*/
|
||||
public class ListConverter<S, T> implements Converter<List<S>, List<T>> {
|
||||
|
||||
private Converter<S, T> itemConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param itemConverter
|
||||
* The {@link Converter} to use for converting individual List
|
||||
* items
|
||||
*/
|
||||
public ListConverter(Converter<S, T> itemConverter) {
|
||||
this.itemConverter = itemConverter;
|
||||
}
|
||||
|
||||
public List<T> convert(List<S> source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
List<T> results = new ArrayList<T>();
|
||||
for (S result : source) {
|
||||
results.add(itemConverter.convert(result));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Long, Boolean> {
|
||||
|
||||
public Boolean convert(Long result) {
|
||||
return result != null ? result == 1 : null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <S>
|
||||
* The type of keys and values in the Map to convert
|
||||
* @param <T>
|
||||
* The type of keys and values in the converted Map
|
||||
*/
|
||||
public class MapConverter<S, T> implements Converter<Map<S, S>, Map<T, T>> {
|
||||
|
||||
private Converter<S, T> itemConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param itemConverter
|
||||
* The {@link Converter} to use for converting individual Map
|
||||
* keys and values
|
||||
*/
|
||||
public MapConverter(Converter<S, T> itemConverter) {
|
||||
this.itemConverter = itemConverter;
|
||||
}
|
||||
|
||||
public Map<T, T> convert(Map<S, S> source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
Map<T, T> results;
|
||||
if (source instanceof LinkedHashMap) {
|
||||
results = new LinkedHashMap<T, T>();
|
||||
} else {
|
||||
results = new HashMap<T, T>();
|
||||
}
|
||||
for (Map.Entry<S, S> result : source.entrySet()) {
|
||||
results.put(itemConverter.convert(result.getKey()),
|
||||
itemConverter.convert(result.getValue()));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <S>
|
||||
* The type of elements in the Set to convert
|
||||
* @param <T>
|
||||
* The type of elements in the converted Set
|
||||
*/
|
||||
public class SetConverter<S, T> implements Converter<Set<S>, Set<T>> {
|
||||
|
||||
private Converter<S, T> itemConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param itemConverter
|
||||
* The {@link Converter} to use for converting individual Set
|
||||
* items
|
||||
*/
|
||||
public SetConverter(Converter<S, T> itemConverter) {
|
||||
this.itemConverter = itemConverter;
|
||||
}
|
||||
|
||||
public Set<T> convert(Set<S> source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
Set<T> results;
|
||||
if (source instanceof LinkedHashSet) {
|
||||
results = new LinkedHashSet<T>();
|
||||
} else {
|
||||
results = new HashSet<T>();
|
||||
}
|
||||
for (S result : source) {
|
||||
results.add(itemConverter.convert(result));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, DataType> {
|
||||
|
||||
public DataType convert(String source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
return DataType.fromCode(source);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, Properties> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user