diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2017-02-13 20:22:04 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2017-02-13 20:22:04 +0200 |
commit | 7cf6721a1f730fe6df612d2a43e513e41db59dad (patch) | |
tree | 053def1c4497dfc74afd1ab47a114ac1b4fb243b /container-search | |
parent | cbb619aa5fd837463146e84f5f0dc1c67638621d (diff) |
When code is duplicated is will only be fixed in one of the places.
At least now the cloning magic is only present in one place.
Shall we deprecate/deduplicate any of this processing/search code ?
Diffstat (limited to 'container-search')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/query/properties/CloneHelper.java | 16 | ||||
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/query/properties/PropertyMap.java | 61 |
2 files changed, 20 insertions, 57 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/properties/CloneHelper.java b/container-search/src/main/java/com/yahoo/search/query/properties/CloneHelper.java new file mode 100644 index 00000000000..e0dec5e2d6c --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/properties/CloneHelper.java @@ -0,0 +1,16 @@ +package com.yahoo.search.query.properties; + +import com.yahoo.search.result.Hit; + +/** + * Created by balder on 13/02/2017. + */ +public class CloneHelper extends com.yahoo.processing.request.CloneHelper { + @Override + protected Object objectClone(Object object) { + if (object instanceof Hit) { + return ((Hit)object).clone(); + } + return super.objectClone(object); + } +} diff --git a/container-search/src/main/java/com/yahoo/search/query/properties/PropertyMap.java b/container-search/src/main/java/com/yahoo/search/query/properties/PropertyMap.java index 820c4fc8ea3..934f1cacf32 100644 --- a/container-search/src/main/java/com/yahoo/search/query/properties/PropertyMap.java +++ b/container-search/src/main/java/com/yahoo/search/query/properties/PropertyMap.java @@ -1,17 +1,8 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.search.query.properties; - -import com.yahoo.processing.request.CompoundName; +import com.yahoo.processing.request.*; import com.yahoo.search.query.Properties; -import com.yahoo.search.result.Hit; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.Map; +import java.util.*; import java.util.logging.Logger; /** @@ -29,7 +20,7 @@ import java.util.logging.Logger; */ public class PropertyMap extends Properties { - private static Logger log=Logger.getLogger(PropertyMap.class.getName()); + private final static CloneHelper cloneHelper = new CloneHelper(); /** The properties of this */ private Map<CompoundName, Object> properties = new LinkedHashMap<>(); @@ -64,7 +55,7 @@ public class PropertyMap extends Properties { PropertyMap clone = (PropertyMap)super.clone(); clone.properties = new HashMap<>(); for (Map.Entry<CompoundName, Object> entry : this.properties.entrySet()) { - Object cloneValue = clone(entry.getValue()); + Object cloneValue = cloneHelper.clone(entry.getValue()); if (cloneValue == null) cloneValue = entry.getValue(); // Shallow copy objects which does not support cloning clone.properties.put(entry.getKey(), cloneValue); @@ -72,50 +63,6 @@ public class PropertyMap extends Properties { return clone; } - /** Clones this object if it is clonable, and the clone is public. Returns null if not */ - public static Object clone(Object object) { - if (object==null) return null; - if (! ( object instanceof Cloneable) ) return null; - if (object instanceof Object[]) - return arrayClone((Object[])object); - else - return objectClone(object); - } - - private static Object arrayClone(Object[] object) { - Object[] arrayClone= Arrays.copyOf(object, object.length); - // deep clone - for (int i=0; i<arrayClone.length; i++) { - Object elementClone=clone(arrayClone[i]); - if (elementClone!=null) - arrayClone[i]=elementClone; - } - return arrayClone; - } - - private static Object objectClone(Object object) { - if (object instanceof Hit) { - return ((Hit) object).clone(); - } else if (object instanceof LinkedList) { - return ((LinkedList) object).clone(); - } - try { - Method cloneMethod=object.getClass().getMethod("clone"); - return cloneMethod.invoke(object); - } - catch (NoSuchMethodException e) { - log.warning("'" + object + "' is Cloneable, but has no clone method - will use the same instance in all requests"); - return null; - } - catch (IllegalAccessException e) { - log.warning("'" + object + "' is Cloneable, but clone method cannot be accessed - will use the same instance in all requests"); - return null; - } - catch (InvocationTargetException e) { - throw new RuntimeException("Exception cloning '" + object + "'",e); - } - } - @Override public Map<String, Object> listProperties(CompoundName path, Map<String, String> context, com.yahoo.processing.request.Properties substitution) { Map<String, Object> map = super.listProperties(path, context, substitution); |