X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document.server.io%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fserver%2Fio%2FJSONObjectUtils.java;h=004166dc0c39bf387dd471b9b8617bb8c8853885;hp=8913645a9946c3f4884fd4e8c5ae9f028afaea1a;hb=8360fe5756f40b0f43f673394f1fef098d31627e;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/JSONObjectUtils.java b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/JSONObjectUtils.java index 8913645a9..004166dc0 100644 --- a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/JSONObjectUtils.java +++ b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/JSONObjectUtils.java @@ -13,6 +13,7 @@ package org.simantics.document.server.io; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -313,6 +314,26 @@ public class JSONObjectUtils { return Collections.emptyList(); } + @SuppressWarnings("unchecked") + public static Collection getTreeTableCells(IJSONObject object) { + try { + Object tableCells = object.getValue("tableCells"); + if (tableCells instanceof String) { + String tableCellsS = (String) tableCells; + if (tableCellsS.length() == 0) + return Collections.emptyList(); + } + if (tableCells != null) { + return (List) tableCells; + } else { + return Collections.emptyList(); + } + } catch (ClassCastException e) { + e.printStackTrace(); + } + return Collections.emptyList(); + } + public static Collection getFiles(IJSONObject object) { try { @SuppressWarnings("unchecked") @@ -343,4 +364,72 @@ public class JSONObjectUtils { else return Collections.emptyList(); } + + @SuppressWarnings("unchecked") + public static Collection getTreeTableCell(IJSONObject object) { + try { + Object treeTableCells = object.getValue("tableCells"); + if (treeTableCells instanceof String) { + String tableCellsS = (String) treeTableCells; + if (tableCellsS.length() == 0) + return Collections.emptyList(); + } + if (treeTableCells != null) { + return (List) treeTableCells; + } else { + return Collections.emptyList(); + } + } catch (ClassCastException e) { + e.printStackTrace(); + } + return Collections.emptyList(); + } + + public static final boolean equalObjects(Object oldValue, Object newValue) { + if (newValue != null) { + if (newValue.getClass().isArray()) { + return arrayEquals(newValue, oldValue); + } else { + return newValue.equals(oldValue); + } + } else + return oldValue == null; + } + + + /** + * @param av1 an array (guaranteed) + * @param av2 any object + * @return true if the two arrays are equal + */ + private static final boolean arrayEquals(Object av1, Object av2) { + if (av2 == null) + return false; + Class c1 = av1.getClass().getComponentType(); + Class c2 = av2.getClass().getComponentType(); + if (c2 == null || !c1.equals(c2)) + return false; + boolean p1 = c1.isPrimitive(); + boolean p2 = c2.isPrimitive(); + if (p1 != p2) + return false; + if (!p1) + return Arrays.equals((Object[]) av1, (Object[]) av2); + if (boolean.class.equals(c1)) + return Arrays.equals((boolean[]) av1, (boolean[]) av2); + else if (byte.class.equals(c1)) + return Arrays.equals((byte[]) av1, (byte[]) av2); + else if (int.class.equals(c1)) + return Arrays.equals((int[]) av1, (int[]) av2); + else if (long.class.equals(c1)) + return Arrays.equals((long[]) av1, (long[]) av2); + else if (float.class.equals(c1)) + return Arrays.equals((float[]) av1, (float[]) av2); + else if (double.class.equals(c1)) + return Arrays.equals((double[]) av1, (double[]) av2); + throw new RuntimeException("Unsupported objects for equality testing ." + av1 + " vs. " + av2); + + } + + }