From 5ad93b2e78550932d3664268bbe49c7409cb26f7 Mon Sep 17 00:00:00 2001 From: Albert Shift Date: Wed, 22 Apr 2015 16:01:21 -0700 Subject: [PATCH] refactor transformers in converters --- .../convert/EntityToUDTValueConverter.java | 10 +-- .../convert/ListToUDTListConverter.java | 21 ++---- .../convert/MapToUDTKeyMapConverter.java | 20 ++---- .../mapping/convert/MapToUDTMapConverter.java | 17 +---- .../convert/MapToUDTValueMapConverter.java | 21 ++---- .../mapping/convert/SetToUDTSetConverter.java | 20 ++---- .../mapping/convert/UDTValueWriter.java | 10 +-- .../noorq/casser/support/Transformers.java | 69 +++++++++++++++++++ 8 files changed, 102 insertions(+), 86 deletions(-) create mode 100644 src/main/java/com/noorq/casser/support/Transformers.java diff --git a/src/main/java/com/noorq/casser/mapping/convert/EntityToUDTValueConverter.java b/src/main/java/com/noorq/casser/mapping/convert/EntityToUDTValueConverter.java index d3dfbda..35449fe 100644 --- a/src/main/java/com/noorq/casser/mapping/convert/EntityToUDTValueConverter.java +++ b/src/main/java/com/noorq/casser/mapping/convert/EntityToUDTValueConverter.java @@ -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 { +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); - } } diff --git a/src/main/java/com/noorq/casser/mapping/convert/ListToUDTListConverter.java b/src/main/java/com/noorq/casser/mapping/convert/ListToUDTListConverter.java index fbf3d5e..4e56952 100644 --- a/src/main/java/com/noorq/casser/mapping/convert/ListToUDTListConverter.java +++ b/src/main/java/com/noorq/casser/mapping/convert/ListToUDTListConverter.java @@ -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 { +public final class ListToUDTListConverter implements Function { + 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 sourceSet = (List) t; - - ImmutableList.Builder builder = ImmutableList.builder(); - - for (Object source : sourceSet) { - builder.add(write(source)); - } - - return builder.build(); + return Transformers.transformList((List) t, writer); } - } diff --git a/src/main/java/com/noorq/casser/mapping/convert/MapToUDTKeyMapConverter.java b/src/main/java/com/noorq/casser/mapping/convert/MapToUDTKeyMapConverter.java index 09e9766..51f97aa 100644 --- a/src/main/java/com/noorq/casser/mapping/convert/MapToUDTKeyMapConverter.java +++ b/src/main/java/com/noorq/casser/mapping/convert/MapToUDTKeyMapConverter.java @@ -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 { +public final class MapToUDTKeyMapConverter implements Function { + 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 sourceMap = (Map) t; - - Map out = new HashMap(); - - for (Map.Entry source : sourceMap.entrySet()) { - out.put(write(source.getKey()), source.getValue()); - } - - return out; + return Transformers.transformMapKey((Map) t, writer); } diff --git a/src/main/java/com/noorq/casser/mapping/convert/MapToUDTMapConverter.java b/src/main/java/com/noorq/casser/mapping/convert/MapToUDTMapConverter.java index ad709a3..e2a4c93 100644 --- a/src/main/java/com/noorq/casser/mapping/convert/MapToUDTMapConverter.java +++ b/src/main/java/com/noorq/casser/mapping/convert/MapToUDTMapConverter.java @@ -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 { @@ -35,19 +34,7 @@ public final class MapToUDTMapConverter implements Function { @Override public Object apply(Object t) { - - Map sourceMap = (Map) t; - Map out = new HashMap(); - - for (Map.Entry source : sourceMap.entrySet()) { - - out.put(keyWriter.write(source.getKey()), - valueWriter.write(source.getValue())); - - } - - return out; + return Transformers.transformMap((Map) t, keyWriter, valueWriter); } - } diff --git a/src/main/java/com/noorq/casser/mapping/convert/MapToUDTValueMapConverter.java b/src/main/java/com/noorq/casser/mapping/convert/MapToUDTValueMapConverter.java index eadfa01..c798f42 100644 --- a/src/main/java/com/noorq/casser/mapping/convert/MapToUDTValueMapConverter.java +++ b/src/main/java/com/noorq/casser/mapping/convert/MapToUDTValueMapConverter.java @@ -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 { +public final class MapToUDTValueMapConverter implements Function { + 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 sourceMap = (Map) t; - - Map out = new HashMap(); - - for (Map.Entry source : sourceMap.entrySet()) { - out.put(source.getKey(), write(source.getValue())); - - } - - return out; + return Transformers.transformMapValue((Map) t, writer); } diff --git a/src/main/java/com/noorq/casser/mapping/convert/SetToUDTSetConverter.java b/src/main/java/com/noorq/casser/mapping/convert/SetToUDTSetConverter.java index da8d205..78aab5c 100644 --- a/src/main/java/com/noorq/casser/mapping/convert/SetToUDTSetConverter.java +++ b/src/main/java/com/noorq/casser/mapping/convert/SetToUDTSetConverter.java @@ -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 { +public final class SetToUDTSetConverter implements Function { + 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 sourceSet = (Set) t; - - ImmutableSet.Builder builder = ImmutableSet.builder(); - - for (Object source : sourceSet) { - builder.add(write(source)); - } - - return builder.build(); + return Transformers.transformSet((Set) t, writer); } diff --git a/src/main/java/com/noorq/casser/mapping/convert/UDTValueWriter.java b/src/main/java/com/noorq/casser/mapping/convert/UDTValueWriter.java index 669f0ec..d2c9d66 100644 --- a/src/main/java/com/noorq/casser/mapping/convert/UDTValueWriter.java +++ b/src/main/java/com/noorq/casser/mapping/convert/UDTValueWriter.java @@ -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 { +public class UDTValueWriter extends AbstractEntityValueWriter implements Function { - 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 write(Object source) { + @Override + public UDTValue apply(Object source) { if (source != null) { UDTValue outValue = userType.newValue(); write(outValue, source); diff --git a/src/main/java/com/noorq/casser/support/Transformers.java b/src/main/java/com/noorq/casser/support/Transformers.java new file mode 100644 index 0000000..bd3499f --- /dev/null +++ b/src/main/java/com/noorq/casser/support/Transformers.java @@ -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 Set transformSet(Set inputSet, Function func) { + Set set = Sets.newHashSet(); + for (I in : inputSet) { + set.add(func.apply(in)); + } + return set; + } + + public static List transformList(List inputList, Function func) { + List list = Lists.newArrayList(); + for (I in : inputList) { + list.add(func.apply(in)); + } + return list; + } + + public static Map transformMapKey(Map inputMap, Function func) { + Map map = Maps.newHashMap(); + for (Map.Entry e : inputMap.entrySet()) { + map.put(func.apply(e.getKey()), e.getValue()); + } + return map; + } + + public static Map transformMapValue(Map inputMap, Function func) { + return Maps.transformValues(inputMap, func::apply); + } + + public static Map transformMap(Map inputMap, Function funcKey, Function funcValue) { + Map map = Maps.newHashMap(); + for (Map.Entry e : inputMap.entrySet()) { + map.put(funcKey.apply(e.getKey()), + funcValue.apply(e.getValue())); + } + return map; + } + +}