-package org.simantics.export.core.util;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.Comparator;\r
-import java.util.List;\r
-\r
-import org.osgi.service.prefs.BackingStoreException;\r
-import org.osgi.service.prefs.Preferences;\r
-import org.simantics.databoard.Accessors;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.accessor.Accessor;\r
-import org.simantics.databoard.accessor.RecordAccessor;\r
-import org.simantics.databoard.accessor.UnionAccessor;\r
-import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
-import org.simantics.databoard.accessor.error.AccessorException;\r
-import org.simantics.databoard.accessor.error.ReferenceException;\r
-import org.simantics.databoard.accessor.reference.ChildReference;\r
-import org.simantics.databoard.accessor.reference.LabelReference;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.databoard.type.Component;\r
-import org.simantics.databoard.type.UnionType;\r
-import org.simantics.export.core.ExportContext;\r
-import org.simantics.export.core.ExportExtensionPoint;\r
-import org.simantics.export.core.error.ExportException;\r
-import org.simantics.export.core.intf.Exporter;\r
-import org.simantics.export.core.intf.Publisher;\r
-import org.simantics.export.core.manager.Content;\r
-import org.simantics.utils.datastructures.MapList;\r
-import org.simantics.utils.strings.AlphanumComparator;\r
-\r
-public class ExporterUtils {\r
-\r
- /**\r
- * Gets publisher location options from options variant.\r
- * \r
- * @param ctx\r
- * @param publisherId\r
- * @param options\r
- * @return publisher options or null\r
- * @throws ExportException\r
- */\r
- public static Variant getPublisherLocationOptions(ExportContext ctx, String publisherId, Variant options) throws ExportException {\r
- try { \r
- Publisher publisher = ctx.eep.getPublisher(publisherId);\r
- Variant locationOptions = options.getComponent( new LabelReference( publisher.label() ) );\r
- return locationOptions;\r
- } catch (AccessorConstructionException e) {\r
- throw new ExportException( e ); \r
- }\r
- }\r
- \r
- /**\r
- * Checks if key exists in a preference node\r
- * \r
- * @param pref\r
- * @param key\r
- * @return true if key exists\r
- */\r
- public static boolean containsKey(Preferences pref, String key) {\r
- try {\r
- for (String x : pref.keys()) if ( x.equals(key) ) return true;\r
- } catch (BackingStoreException e) {\r
- e.printStackTrace();\r
- } \r
- return false; \r
- }\r
- \r
- /**\r
- * Get value of a key, in a preference node.\r
- * \r
- * @param pref (optional)\r
- * @param key key\r
- * @return if pref and value exists, the value.\r
- */\r
- public static String getPrefString(Preferences pref, String key) {\r
- if ( pref == null ) return null;\r
- if ( !containsKey(pref, key) ) return null;\r
- return pref.get(key, null);\r
- }\r
-\r
- /**\r
- * Get value first from pref1, then pref2\r
- * @param pref1 (optional) pririty preference node\r
- * @param pref2 (optional) 2nd priority node\r
- * @param key \r
- * @return possible value\r
- */\r
- public static String getPrefString(Preferences pref1, Preferences pref2, String key) {\r
- if ( pref1 != null && containsKey(pref1, key) ) return pref1.get(key, null);\r
- if ( pref2 != null && containsKey(pref2, key) ) return pref2.get(key, null);\r
- return null;\r
- }\r
-\r
- /**\r
- * Get value first from pref1, then pref2\r
- * @param pref1 (optional) pririty preference node\r
- * @param pref2 (optional) 2nd priority node\r
- * @param key \r
- * @return possible value\r
- */\r
- public static Integer getPrefInteger(Preferences pref1, Preferences pref2, String key) {\r
- if ( pref1 != null && containsKey(pref1, key) ) return pref1.getInt(key, -1);\r
- if ( pref2 != null && containsKey(pref2, key) ) return pref2.getInt(key, -1);\r
- return null;\r
- }\r
-\r
- /**\r
- * Get value first from pref1, then pref2\r
- * @param pref1 (optional) pririty preference node\r
- * @param pref2 (optional) 2nd priority node\r
- * @param key \r
- * @return possible value\r
- */\r
- public static Boolean getPrefBoolean(Preferences pref1, Preferences pref2, String key) {\r
- if ( pref1 != null && containsKey(pref1, key) ) return pref1.getBoolean(key, false);\r
- if ( pref2 != null && containsKey(pref2, key) ) return pref2.getBoolean(key, false);\r
- return null;\r
- }\r
- \r
- /**\r
- * Get value first from pref1, then pref2\r
- * @param pref1 (optional) pririty preference node\r
- * @param pref2 (optional) 2nd priority node\r
- * @param key \r
- * @return possible value\r
- */\r
- public static Double getPrefDouble(Preferences pref1, Preferences pref2, String key) {\r
- if ( pref1 != null && containsKey(pref1, key) ) return pref1.getDouble(key, 0.0);\r
- if ( pref2 != null && containsKey(pref2, key) ) return pref2.getDouble(key, 0.0);\r
- return null;\r
- }\r
- \r
- public static void setPrefString(Preferences pref, String key, String value) {\r
- if ( pref == null ) return;\r
- if ( value == null ) {\r
- pref.remove(key); \r
- } else {\r
- pref.put(key, value);\r
- }\r
- }\r
- \r
- public static void setPrefInt(Preferences pref, String key, Integer value) {\r
- if ( pref == null ) return;\r
- if ( value == null ) {\r
- pref.remove(key); \r
- } else {\r
- pref.putInt(key, value);\r
- }\r
- }\r
- \r
- public static void setPrefdouble(Preferences pref, String key, Double value) {\r
- if ( pref == null ) return;\r
- if ( value == null ) {\r
- pref.remove(key); \r
- } else {\r
- pref.putDouble(key, value);\r
- }\r
- }\r
-\r
- /**\r
- * Set enum or union value to an accessor, using the string name of the enum value.\r
- * If value doesn't exist, false is returned.\r
- * If tagName doesn't exist, false is returned.\r
- * \r
- * @param ra accessor\r
- * @param ref child reference\r
- * @param tagName (optional) the tag name\r
- * @return true if value was set\r
- */\r
- public static boolean setUnionValue(Accessor ra, ChildReference ref, String tagName) {\r
- if ( tagName == null ) return false;\r
- try { \r
- UnionAccessor ua = ra.getComponent( ref );\r
- UnionType ut = ua.type(); \r
- int tag = ut.getComponentIndex2( tagName );\r
- if ( tag<0 ) return false;\r
- Component c = ut.components[tag];\r
- Binding cb = Bindings.getMutableBinding(c.type);\r
- Object o = cb.createDefault();\r
- ua.setComponentValue( tag, cb, o);\r
- return true;\r
- } catch (ReferenceException re) { \r
- return false;\r
- } catch (AccessorConstructionException e) {\r
- e.printStackTrace();\r
- return false;\r
- } catch (AccessorException e) {\r
- e.printStackTrace();\r
- return false;\r
- } catch (BindingException e) {\r
- e.printStackTrace();\r
- return false;\r
- } \r
- }\r
-\r
- /**\r
- * Set enum or union value to an accessor, using the string name of the enum value.\r
- * If value doesn't exist, false is returned.\r
- * If tagName doesn't exist, false is returned.\r
- * \r
- * @param ra accessor\r
- * @param ref child reference\r
- * @param tag the tag number (applied only if >=0)\r
- * @return true if value was set\r
- */\r
- public static boolean setUnionValue(Accessor ra, ChildReference ref, int tag) {\r
- if ( tag<0 ) return false;\r
- try { \r
- UnionAccessor ua = ra.getComponent( ref );\r
- UnionType ut = ua.type(); \r
- Component c = ut.components[tag];\r
- Binding cb = Bindings.getMutableBinding(c.type);\r
- Object o = cb.createDefault();\r
- ua.setComponentValue( tag, cb, o);\r
- return true;\r
- } catch (ReferenceException re) { \r
- return false;\r
- } catch (AccessorConstructionException e) {\r
- e.printStackTrace();\r
- return false;\r
- } catch (AccessorException e) {\r
- e.printStackTrace();\r
- return false;\r
- } catch (BindingException e) {\r
- e.printStackTrace();\r
- return false;\r
- } \r
- }\r
- \r
- \r
- /**\r
- * Get union value as a String\r
- * \r
- * @param ra\r
- * @param ref\r
- * @return the union value as string or null if error occured\r
- */\r
- public static String getUnionValue(Accessor ra, ChildReference ref) {\r
- try { \r
- UnionAccessor ua = ra.getComponent( ref );\r
- UnionType ut = ua.type();\r
- int tag = ua.getTag();\r
- return ut.components[tag].name;\r
- } catch (ReferenceException re) { \r
- return null;\r
- } catch (AccessorConstructionException e) {\r
- e.printStackTrace();\r
- return null;\r
- } catch (AccessorException e) {\r
- e.printStackTrace();\r
- return null;\r
- } \r
- \r
- }\r
-\r
- /**\r
- * \r
- * @param ra\r
- * @param ref\r
- * @return tag or -1\r
- */\r
- public static int getUnionInt(Accessor ra, ChildReference ref) {\r
- try { \r
- UnionAccessor ua = ra.getComponent( ref );\r
- int tag = ua.getTag();\r
- return tag;\r
- } catch (ReferenceException re) { \r
- return -1;\r
- } catch (AccessorConstructionException e) {\r
- e.printStackTrace();\r
- return -1;\r
- } catch (AccessorException e) {\r
- e.printStackTrace();\r
- return -1;\r
- } \r
- \r
- }\r
- \r
- /**\r
- * Read string value from an location\r
- * \r
- * @param location\r
- * @param ref\r
- * @return the value or null, if did not exist\r
- */\r
- public static String getString(Variant location, ChildReference ref) {\r
- try {\r
- RecordAccessor ra = Accessors.getAccessor(location);\r
- return (String) ra.getValue( ref, Bindings.STRING );\r
- } catch (AccessorConstructionException e) {\r
- return null;\r
- } catch (AccessorException e) {\r
- return null;\r
- } \r
- }\r
- \r
- public static Boolean getBoolean(Variant location, ChildReference ref) {\r
- try {\r
- RecordAccessor ra = Accessors.getAccessor(location);\r
- return (Boolean) ra.getValue( ref, Bindings.BOOLEAN );\r
- } catch (AccessorConstructionException e) {\r
- return null;\r
- } catch (AccessorException e) {\r
- return null;\r
- } \r
- }\r
- \r
- public static Comparator<Content> createExportPriorityComparator( final ExportExtensionPoint eep ) {\r
- return new Comparator<Content>() {\r
-\r
- @Override\r
- public int compare(Content c1, Content c2) {\r
- \r
- int p1 = Integer.MAX_VALUE;\r
- for (Exporter e : eep.getExporters(c1.formatId, c1.contentTypeId)) \r
- p1 = Math.min(p1, e.getExportPriority());\r
- \r
- int p2 = Integer.MAX_VALUE;\r
- for (Exporter e : eep.getExporters(c2.formatId, c2.contentTypeId)) \r
- p2 = Math.min(p2, e.getExportPriority());\r
- \r
- return Integer.signum( p1 - p2 );\r
- }\r
- \r
- }; \r
- }\r
- \r
- /**\r
- * Takes a maplist of contents as input and filters out all that matches\r
- * to the given uri. \r
- * \r
- * @param map\r
- * @param contentUri\r
- * @return map with contents of given Uri.\r
- */\r
- public static MapList<Content, Content> filterByUri(MapList<Content, Content> map, String contentUri) {\r
- if ( map == null ) return null;\r
- MapList<Content, Content> result = new MapList<Content, Content>();\r
- for ( Content key : map.getKeys() ) { \r
- if ( key.url.equals(contentUri) ) {\r
- result.addAll(key, map.getValuesUnsafe(key));\r
- }\r
- }\r
- return result;\r
- }\r
- \r
- public static MapList<String, Content> toStringMap(MapList<Content, Content> map) {\r
- if ( map == null ) return null;\r
- MapList<String, Content> result = new MapList<String, Content>();\r
- for ( Content key : map.getKeys() ) {\r
- result.addAll(key.url, map.getValuesUnsafe(key));\r
- } \r
- return result;\r
- }\r
- \r
- public static List<Content> filterContents(Collection<Content> contents, String contentTypeId, String formatId) {\r
- if ( contents==null ) return null;\r
- List<Content> result = new ArrayList<Content>(contents.size());\r
- for ( Content content : contents ) {\r
- if ( contentTypeId!=null && !content.contentTypeId.equals(contentTypeId) ) continue;\r
- if ( formatId!=null && !content.formatId.equals(formatId) ) continue;\r
- result.add( content );\r
- }\r
- return result;\r
- }\r
- \r
- public static List<String> toUris(Collection<Content> contents) {\r
- List<String> result = new ArrayList<String>(contents.size());\r
- for ( Content content : contents ) result.add( content.url ); \r
- return result;\r
- } \r
-\r
- public static Comparator<Content> CONTENT_COMPARATOR = new Comparator<Content>() {\r
- @Override\r
- public int compare(Content o1, Content o2) {\r
- int c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.url, o2.url);\r
- if (c != 0)\r
- return c;\r
- c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.formatId, o2.formatId);\r
- if (c != 0)\r
- return c;\r
- return 0;\r
- }\r
- };\r
-\r
- public static List<Content> sortContent(List<Content> content) {\r
- Collections.sort(content, CONTENT_COMPARATOR);\r
- return content;\r
- }\r
-\r
- public static List<Content> sortedContent(List<Content> content) {\r
- ArrayList<Content> sorted = new ArrayList<Content>(content);\r
- sortContent(sorted);\r
- return sorted;\r
- }\r
-\r
-}\r
+package org.simantics.export.core.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
+import org.simantics.databoard.Accessors;
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.accessor.Accessor;
+import org.simantics.databoard.accessor.RecordAccessor;
+import org.simantics.databoard.accessor.UnionAccessor;
+import org.simantics.databoard.accessor.error.AccessorConstructionException;
+import org.simantics.databoard.accessor.error.AccessorException;
+import org.simantics.databoard.accessor.error.ReferenceException;
+import org.simantics.databoard.accessor.reference.ChildReference;
+import org.simantics.databoard.accessor.reference.LabelReference;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.databoard.type.Component;
+import org.simantics.databoard.type.UnionType;
+import org.simantics.export.core.ExportContext;
+import org.simantics.export.core.ExportExtensionPoint;
+import org.simantics.export.core.error.ExportException;
+import org.simantics.export.core.intf.Exporter;
+import org.simantics.export.core.intf.Publisher;
+import org.simantics.export.core.manager.Content;
+import org.simantics.utils.datastructures.MapList;
+import org.simantics.utils.strings.AlphanumComparator;
+
+public class ExporterUtils {
+
+ /**
+ * Gets publisher location options from options variant.
+ *
+ * @param ctx
+ * @param publisherId
+ * @param options
+ * @return publisher options or null
+ * @throws ExportException
+ */
+ public static Variant getPublisherLocationOptions(ExportContext ctx, String publisherId, Variant options) throws ExportException {
+ try {
+ Publisher publisher = ctx.eep.getPublisher(publisherId);
+ Variant locationOptions = options.getComponent( new LabelReference( publisher.label() ) );
+ return locationOptions;
+ } catch (AccessorConstructionException e) {
+ throw new ExportException( e );
+ }
+ }
+
+ /**
+ * Checks if key exists in a preference node
+ *
+ * @param pref
+ * @param key
+ * @return true if key exists
+ */
+ public static boolean containsKey(Preferences pref, String key) {
+ try {
+ for (String x : pref.keys()) if ( x.equals(key) ) return true;
+ } catch (BackingStoreException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ /**
+ * Get value of a key, in a preference node.
+ *
+ * @param pref (optional)
+ * @param key key
+ * @return if pref and value exists, the value.
+ */
+ public static String getPrefString(Preferences pref, String key) {
+ if ( pref == null ) return null;
+ if ( !containsKey(pref, key) ) return null;
+ return pref.get(key, null);
+ }
+
+ /**
+ * Get value first from pref1, then pref2
+ * @param pref1 (optional) pririty preference node
+ * @param pref2 (optional) 2nd priority node
+ * @param key
+ * @return possible value
+ */
+ public static String getPrefString(Preferences pref1, Preferences pref2, String key) {
+ if ( pref1 != null && containsKey(pref1, key) ) return pref1.get(key, null);
+ if ( pref2 != null && containsKey(pref2, key) ) return pref2.get(key, null);
+ return null;
+ }
+
+ /**
+ * Get value first from pref1, then pref2
+ * @param pref1 (optional) pririty preference node
+ * @param pref2 (optional) 2nd priority node
+ * @param key
+ * @return possible value
+ */
+ public static Integer getPrefInteger(Preferences pref1, Preferences pref2, String key) {
+ if ( pref1 != null && containsKey(pref1, key) ) return pref1.getInt(key, -1);
+ if ( pref2 != null && containsKey(pref2, key) ) return pref2.getInt(key, -1);
+ return null;
+ }
+
+ /**
+ * Get value first from pref1, then pref2
+ * @param pref1 (optional) pririty preference node
+ * @param pref2 (optional) 2nd priority node
+ * @param key
+ * @return possible value
+ */
+ public static Boolean getPrefBoolean(Preferences pref1, Preferences pref2, String key) {
+ if ( pref1 != null && containsKey(pref1, key) ) return pref1.getBoolean(key, false);
+ if ( pref2 != null && containsKey(pref2, key) ) return pref2.getBoolean(key, false);
+ return null;
+ }
+
+ /**
+ * Get value first from pref1, then pref2
+ * @param pref1 (optional) pririty preference node
+ * @param pref2 (optional) 2nd priority node
+ * @param key
+ * @return possible value
+ */
+ public static Double getPrefDouble(Preferences pref1, Preferences pref2, String key) {
+ if ( pref1 != null && containsKey(pref1, key) ) return pref1.getDouble(key, 0.0);
+ if ( pref2 != null && containsKey(pref2, key) ) return pref2.getDouble(key, 0.0);
+ return null;
+ }
+
+ public static void setPrefString(Preferences pref, String key, String value) {
+ if ( pref == null ) return;
+ if ( value == null ) {
+ pref.remove(key);
+ } else {
+ pref.put(key, value);
+ }
+ }
+
+ public static void setPrefInt(Preferences pref, String key, Integer value) {
+ if ( pref == null ) return;
+ if ( value == null ) {
+ pref.remove(key);
+ } else {
+ pref.putInt(key, value);
+ }
+ }
+
+ public static void setPrefdouble(Preferences pref, String key, Double value) {
+ if ( pref == null ) return;
+ if ( value == null ) {
+ pref.remove(key);
+ } else {
+ pref.putDouble(key, value);
+ }
+ }
+
+ /**
+ * Set enum or union value to an accessor, using the string name of the enum value.
+ * If value doesn't exist, false is returned.
+ * If tagName doesn't exist, false is returned.
+ *
+ * @param ra accessor
+ * @param ref child reference
+ * @param tagName (optional) the tag name
+ * @return true if value was set
+ */
+ public static boolean setUnionValue(Accessor ra, ChildReference ref, String tagName) {
+ if ( tagName == null ) return false;
+ try {
+ UnionAccessor ua = ra.getComponent( ref );
+ UnionType ut = ua.type();
+ int tag = ut.getComponentIndex2( tagName );
+ if ( tag<0 ) return false;
+ Component c = ut.components[tag];
+ Binding cb = Bindings.getMutableBinding(c.type);
+ Object o = cb.createDefault();
+ ua.setComponentValue( tag, cb, o);
+ return true;
+ } catch (ReferenceException re) {
+ return false;
+ } catch (AccessorConstructionException e) {
+ e.printStackTrace();
+ return false;
+ } catch (AccessorException e) {
+ e.printStackTrace();
+ return false;
+ } catch (BindingException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ /**
+ * Set enum or union value to an accessor, using the string name of the enum value.
+ * If value doesn't exist, false is returned.
+ * If tagName doesn't exist, false is returned.
+ *
+ * @param ra accessor
+ * @param ref child reference
+ * @param tag the tag number (applied only if >=0)
+ * @return true if value was set
+ */
+ public static boolean setUnionValue(Accessor ra, ChildReference ref, int tag) {
+ if ( tag<0 ) return false;
+ try {
+ UnionAccessor ua = ra.getComponent( ref );
+ UnionType ut = ua.type();
+ Component c = ut.components[tag];
+ Binding cb = Bindings.getMutableBinding(c.type);
+ Object o = cb.createDefault();
+ ua.setComponentValue( tag, cb, o);
+ return true;
+ } catch (ReferenceException re) {
+ return false;
+ } catch (AccessorConstructionException e) {
+ e.printStackTrace();
+ return false;
+ } catch (AccessorException e) {
+ e.printStackTrace();
+ return false;
+ } catch (BindingException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * Get union value as a String
+ *
+ * @param ra
+ * @param ref
+ * @return the union value as string or null if error occured
+ */
+ public static String getUnionValue(Accessor ra, ChildReference ref) {
+ try {
+ UnionAccessor ua = ra.getComponent( ref );
+ UnionType ut = ua.type();
+ int tag = ua.getTag();
+ return ut.components[tag].name;
+ } catch (ReferenceException re) {
+ return null;
+ } catch (AccessorConstructionException e) {
+ e.printStackTrace();
+ return null;
+ } catch (AccessorException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ }
+
+ /**
+ *
+ * @param ra
+ * @param ref
+ * @return tag or -1
+ */
+ public static int getUnionInt(Accessor ra, ChildReference ref) {
+ try {
+ UnionAccessor ua = ra.getComponent( ref );
+ int tag = ua.getTag();
+ return tag;
+ } catch (ReferenceException re) {
+ return -1;
+ } catch (AccessorConstructionException e) {
+ e.printStackTrace();
+ return -1;
+ } catch (AccessorException e) {
+ e.printStackTrace();
+ return -1;
+ }
+
+ }
+
+ /**
+ * Read string value from an location
+ *
+ * @param location
+ * @param ref
+ * @return the value or null, if did not exist
+ */
+ public static String getString(Variant location, ChildReference ref) {
+ try {
+ RecordAccessor ra = Accessors.getAccessor(location);
+ return (String) ra.getValue( ref, Bindings.STRING );
+ } catch (AccessorConstructionException e) {
+ return null;
+ } catch (AccessorException e) {
+ return null;
+ }
+ }
+
+ public static Boolean getBoolean(Variant location, ChildReference ref) {
+ try {
+ RecordAccessor ra = Accessors.getAccessor(location);
+ return (Boolean) ra.getValue( ref, Bindings.BOOLEAN );
+ } catch (AccessorConstructionException e) {
+ return null;
+ } catch (AccessorException e) {
+ return null;
+ }
+ }
+
+ public static Comparator<Content> createExportPriorityComparator( final ExportExtensionPoint eep ) {
+ return new Comparator<Content>() {
+
+ @Override
+ public int compare(Content c1, Content c2) {
+
+ int p1 = Integer.MAX_VALUE;
+ for (Exporter e : eep.getExporters(c1.formatId, c1.contentTypeId))
+ p1 = Math.min(p1, e.getExportPriority());
+
+ int p2 = Integer.MAX_VALUE;
+ for (Exporter e : eep.getExporters(c2.formatId, c2.contentTypeId))
+ p2 = Math.min(p2, e.getExportPriority());
+
+ return Integer.signum( p1 - p2 );
+ }
+
+ };
+ }
+
+ /**
+ * Takes a maplist of contents as input and filters out all that matches
+ * to the given uri.
+ *
+ * @param map
+ * @param contentUri
+ * @return map with contents of given Uri.
+ */
+ public static MapList<Content, Content> filterByUri(MapList<Content, Content> map, String contentUri) {
+ if ( map == null ) return null;
+ MapList<Content, Content> result = new MapList<Content, Content>();
+ for ( Content key : map.getKeys() ) {
+ if ( key.url.equals(contentUri) ) {
+ result.addAll(key, map.getValuesUnsafe(key));
+ }
+ }
+ return result;
+ }
+
+ public static MapList<String, Content> toStringMap(MapList<Content, Content> map) {
+ if ( map == null ) return null;
+ MapList<String, Content> result = new MapList<String, Content>();
+ for ( Content key : map.getKeys() ) {
+ result.addAll(key.url, map.getValuesUnsafe(key));
+ }
+ return result;
+ }
+
+ public static List<Content> filterContents(Collection<Content> contents, String contentTypeId, String formatId) {
+ if ( contents==null ) return null;
+ List<Content> result = new ArrayList<Content>(contents.size());
+ for ( Content content : contents ) {
+ if ( contentTypeId!=null && !content.contentTypeId.equals(contentTypeId) ) continue;
+ if ( formatId!=null && !content.formatId.equals(formatId) ) continue;
+ result.add( content );
+ }
+ return result;
+ }
+
+ public static List<String> toUris(Collection<Content> contents) {
+ List<String> result = new ArrayList<String>(contents.size());
+ for ( Content content : contents ) result.add( content.url );
+ return result;
+ }
+
+ public static Comparator<Content> CONTENT_COMPARATOR = new Comparator<Content>() {
+ @Override
+ public int compare(Content o1, Content o2) {
+ int c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.url, o2.url);
+ if (c != 0)
+ return c;
+ c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.formatId, o2.formatId);
+ if (c != 0)
+ return c;
+ return 0;
+ }
+ };
+
+ public static List<Content> sortContent(List<Content> content) {
+ Collections.sort(content, CONTENT_COMPARATOR);
+ return content;
+ }
+
+ public static List<Content> sortedContent(List<Content> content) {
+ ArrayList<Content> sorted = new ArrayList<Content>(content);
+ sortContent(sorted);
+ return sorted;
+ }
+
+}