refactor transformers in converters
This commit is contained in:
parent
c6d0043aa6
commit
5ad93b2e78
8 changed files with 102 additions and 86 deletions
|
@ -15,21 +15,13 @@
|
|||
*/
|
||||
package com.noorq.casser.mapping.convert;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
|
||||
public final class EntityToUDTValueConverter extends UDTValueWriter implements Function<Object, UDTValue> {
|
||||
public final class EntityToUDTValueConverter extends UDTValueWriter {
|
||||
|
||||
public EntityToUDTValueConverter(Class<?> iface, UserType userType, SessionRepository repository) {
|
||||
super(iface, userType, repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UDTValue apply(Object source) {
|
||||
return write(source);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,30 +18,21 @@ package com.noorq.casser.mapping.convert;
|
|||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.support.Transformers;
|
||||
|
||||
public final class ListToUDTListConverter extends UDTValueWriter implements Function<Object, Object> {
|
||||
public final class ListToUDTListConverter implements Function<Object, Object> {
|
||||
|
||||
final UDTValueWriter writer;
|
||||
|
||||
public ListToUDTListConverter(Class<?> iface, UserType userType, SessionRepository repository) {
|
||||
super(iface, userType, repository);
|
||||
this.writer = new UDTValueWriter(iface, userType, repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(Object t) {
|
||||
|
||||
List<Object> sourceSet = (List<Object>) t;
|
||||
|
||||
ImmutableList.Builder<UDTValue> builder = ImmutableList.builder();
|
||||
|
||||
for (Object source : sourceSet) {
|
||||
builder.add(write(source));
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
return Transformers.transformList((List<Object>) t, writer);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -15,32 +15,24 @@
|
|||
*/
|
||||
package com.noorq.casser.mapping.convert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.support.Transformers;
|
||||
|
||||
public final class MapToUDTKeyMapConverter extends UDTValueWriter implements Function<Object, Object> {
|
||||
public final class MapToUDTKeyMapConverter implements Function<Object, Object> {
|
||||
|
||||
final UDTValueWriter writer;
|
||||
|
||||
public MapToUDTKeyMapConverter(Class<?> iface, UserType userType, SessionRepository repository) {
|
||||
super(iface, userType, repository);
|
||||
this.writer = new UDTValueWriter(iface, userType, repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(Object t) {
|
||||
|
||||
Map<Object, Object> sourceMap = (Map<Object, Object>) t;
|
||||
|
||||
Map<UDTValue, Object> out = new HashMap<UDTValue, Object>();
|
||||
|
||||
for (Map.Entry<Object, Object> source : sourceMap.entrySet()) {
|
||||
out.put(write(source.getKey()), source.getValue());
|
||||
}
|
||||
|
||||
return out;
|
||||
return Transformers.transformMapKey((Map<Object, Object>) t, writer);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,13 +15,12 @@
|
|||
*/
|
||||
package com.noorq.casser.mapping.convert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.support.Transformers;
|
||||
|
||||
public final class MapToUDTMapConverter implements Function<Object, Object> {
|
||||
|
||||
|
@ -35,19 +34,7 @@ public final class MapToUDTMapConverter implements Function<Object, Object> {
|
|||
|
||||
@Override
|
||||
public Object apply(Object t) {
|
||||
|
||||
Map<Object, Object> sourceMap = (Map<Object, Object>) t;
|
||||
Map<UDTValue, UDTValue> out = new HashMap<UDTValue, UDTValue>();
|
||||
|
||||
for (Map.Entry<Object, Object> source : sourceMap.entrySet()) {
|
||||
|
||||
out.put(keyWriter.write(source.getKey()),
|
||||
valueWriter.write(source.getValue()));
|
||||
|
||||
}
|
||||
|
||||
return out;
|
||||
return Transformers.transformMap((Map<Object, Object>) t, keyWriter, valueWriter);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -15,33 +15,24 @@
|
|||
*/
|
||||
package com.noorq.casser.mapping.convert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.support.Transformers;
|
||||
|
||||
public final class MapToUDTValueMapConverter extends UDTValueWriter implements Function<Object, Object> {
|
||||
public final class MapToUDTValueMapConverter implements Function<Object, Object> {
|
||||
|
||||
final UDTValueWriter writer;
|
||||
|
||||
public MapToUDTValueMapConverter(Class<?> iface, UserType userType, SessionRepository repository) {
|
||||
super(iface, userType, repository);
|
||||
this.writer = new UDTValueWriter(iface, userType, repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(Object t) {
|
||||
|
||||
Map<Object, Object> sourceMap = (Map<Object, Object>) t;
|
||||
|
||||
Map<Object, UDTValue> out = new HashMap<Object, UDTValue>();
|
||||
|
||||
for (Map.Entry<Object, Object> source : sourceMap.entrySet()) {
|
||||
out.put(source.getKey(), write(source.getValue()));
|
||||
|
||||
}
|
||||
|
||||
return out;
|
||||
return Transformers.transformMapValue((Map<Object, Object>) t, writer);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -18,29 +18,21 @@ package com.noorq.casser.mapping.convert;
|
|||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.noorq.casser.core.SessionRepository;
|
||||
import com.noorq.casser.support.Transformers;
|
||||
|
||||
public final class SetToUDTSetConverter extends UDTValueWriter implements Function<Object, Object> {
|
||||
public final class SetToUDTSetConverter implements Function<Object, Object> {
|
||||
|
||||
final UDTValueWriter writer;
|
||||
|
||||
public SetToUDTSetConverter(Class<?> iface, UserType userType, SessionRepository repository) {
|
||||
super(iface, userType, repository);
|
||||
this.writer = new UDTValueWriter(iface, userType, repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(Object t) {
|
||||
|
||||
Set<Object> sourceSet = (Set<Object>) t;
|
||||
|
||||
ImmutableSet.Builder<UDTValue> builder = ImmutableSet.builder();
|
||||
|
||||
for (Object source : sourceSet) {
|
||||
builder.add(write(source));
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
return Transformers.transformSet((Set<Object>) t, writer);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package com.noorq.casser.mapping.convert;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
|
@ -23,10 +24,10 @@ import com.noorq.casser.core.SessionRepository;
|
|||
import com.noorq.casser.mapping.CasserProperty;
|
||||
import com.noorq.casser.mapping.value.UDTColumnValuePreparer;
|
||||
|
||||
public class UDTValueWriter extends AbstractEntityValueWriter<UDTValue> {
|
||||
public class UDTValueWriter extends AbstractEntityValueWriter<UDTValue> implements Function<Object, UDTValue> {
|
||||
|
||||
protected final UserType userType;
|
||||
protected final UDTColumnValuePreparer valuePreparer;
|
||||
final UserType userType;
|
||||
final UDTColumnValuePreparer valuePreparer;
|
||||
|
||||
public UDTValueWriter(Class<?> iface, UserType userType, SessionRepository repository) {
|
||||
super(iface);
|
||||
|
@ -46,7 +47,8 @@ public class UDTValueWriter extends AbstractEntityValueWriter<UDTValue> {
|
|||
}
|
||||
}
|
||||
|
||||
UDTValue write(Object source) {
|
||||
@Override
|
||||
public UDTValue apply(Object source) {
|
||||
if (source != null) {
|
||||
UDTValue outValue = userType.newValue();
|
||||
write(outValue, source);
|
||||
|
|
69
src/main/java/com/noorq/casser/support/Transformers.java
Normal file
69
src/main/java/com/noorq/casser/support/Transformers.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (C) 2015 Noorq, Inc.
|
||||
*
|
||||
* 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 com.noorq.casser.support;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
public final class Transformers {
|
||||
|
||||
private Transformers() {
|
||||
}
|
||||
|
||||
public static <I, O> Set<O> transformSet(Set<I> inputSet, Function<I, O> func) {
|
||||
Set<O> set = Sets.newHashSet();
|
||||
for (I in : inputSet) {
|
||||
set.add(func.apply(in));
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
public static <I, O> List<O> transformList(List<I> inputList, Function<I, O> func) {
|
||||
List<O> list = Lists.newArrayList();
|
||||
for (I in : inputList) {
|
||||
list.add(func.apply(in));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static <I, O, V> Map<O, V> transformMapKey(Map<I, V> inputMap, Function<I, O> func) {
|
||||
Map<O, V> map = Maps.newHashMap();
|
||||
for (Map.Entry<I, V> e : inputMap.entrySet()) {
|
||||
map.put(func.apply(e.getKey()), e.getValue());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <I, O, K> Map<K, O> transformMapValue(Map<K, I> inputMap, Function<I, O> func) {
|
||||
return Maps.transformValues(inputMap, func::apply);
|
||||
}
|
||||
|
||||
public static <X, Y, K, V> Map<X, Y> transformMap(Map<K, V> inputMap, Function<K, X> funcKey, Function<V, Y> funcValue) {
|
||||
Map<X, Y> map = Maps.newHashMap();
|
||||
for (Map.Entry<K, V> e : inputMap.entrySet()) {
|
||||
map.put(funcKey.apply(e.getKey()),
|
||||
funcValue.apply(e.getValue()));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue