DATAKV-36

+ add Apache Commons BeanUtils impl
This commit is contained in:
Costin Leau
2011-03-09 20:40:21 +02:00
parent 2b3018f410
commit 4e55f6d6ce
4 changed files with 144 additions and 1 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2011 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.keyvalue.redis.hash;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
/**
* HashMapper based on Apache Commons BeanUtils project. Does NOT supports nested properties.
*
* @author Costin Leau
*/
public class BeanUtilsHashMapper<T> implements HashMapper<T, String, String> {
private Class<T> type;
public BeanUtilsHashMapper(Class<T> type) {
this.type = type;
}
@Override
public T fromHash(Map<String, String> hash) {
T instance = org.springframework.beans.BeanUtils.instantiate(type);
try {
BeanUtils.populate(instance, hash);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return instance;
}
@Override
public Map<String, String> toHash(T object) {
try {
return BeanUtils.describe(object);
} catch (Exception ex) {
throw new IllegalArgumentException("Cannot describe object " + object);
}
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2011 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.keyvalue.redis.hash;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Delegating hash mapper used for flattening objects into Strings.
* Suitable when dealing with mappers that support Strings and type conversion.
*
* @author Costin Leau
*/
public class DecoratingStringHashMapper<T> implements HashMapper<T, String, String> {
private final HashMapper<T, ?, ?> delegate;
public <K, V> DecoratingStringHashMapper(HashMapper<T, K, V> mapper) {
this.delegate = mapper;
}
@SuppressWarnings("unchecked")
@Override
public T fromHash(Map<String, String> hash) {
Map h = hash;
return delegate.fromHash(h);
}
@Override
public Map<String, String> toHash(T object) {
Map<?, ?> hash = delegate.toHash(object);
Map<String, String> flatten = new LinkedHashMap<String, String>(hash.size());
for (Map.Entry<?, ?> entry : hash.entrySet()) {
flatten.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
return flatten;
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2011 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.keyvalue.redis.mapping;
import org.junit.Test;
import org.springframework.data.keyvalue.redis.hash.BeanUtilsHashMapper;
import org.springframework.data.keyvalue.redis.hash.HashMapper;
/**
* @author Costin Leau
*/
public class BeanUtilsHashMapperTest extends AbstractHashMapperTest {
@Override
protected HashMapper mapperFor(Class t) {
return new BeanUtilsHashMapper(t);
}
@Test(expected = IllegalArgumentException.class)
public void testNestedBean() throws Exception {
super.testNestedBean();
}
}

View File

@@ -23,4 +23,6 @@ Import-Template:
redis.clients.jedis.*;version=${jedis.range},
redis.clients.util.*;version=${jedis.range},
org.apache.commons.pool.impl.*;version="[1.0.0, 3.0.0)",
org.codehaus.jackson.*;version=${jackson.range}
org.codehaus.jackson.*;version=${jackson.range},
org.apache.commons.beanutils.*;version="[1.8.0, 2.0.0)"