Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #166 and #171 with improved graph bridge #172

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 160 additions & 15 deletions src/main/java/org/gephi/graph/api/AttributeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/

package org.gephi.graph.api;

import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
Expand Down Expand Up @@ -41,21 +42,6 @@
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import it.unimi.dsi.fastutil.shorts.ShortOpenHashSet;
import java.time.format.DateTimeParseException;
import org.gephi.graph.impl.TimestampsParser;
import org.gephi.graph.impl.IntervalsParser;
import org.gephi.graph.impl.FormattingAndParsingUtils;
import org.gephi.graph.api.types.TimestampMap;
import org.gephi.graph.api.types.TimestampShortMap;
import org.gephi.graph.api.types.TimestampLongMap;
import org.gephi.graph.api.types.TimestampSet;
import org.gephi.graph.api.types.TimestampCharMap;
import org.gephi.graph.api.types.TimestampDoubleMap;
import org.gephi.graph.api.types.TimestampBooleanMap;
import org.gephi.graph.api.types.TimestampFloatMap;
import org.gephi.graph.api.types.TimestampStringMap;
import org.gephi.graph.api.types.TimestampByteMap;
import org.gephi.graph.api.types.TimestampIntegerMap;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -67,6 +53,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -88,8 +75,22 @@
import org.gephi.graph.api.types.IntervalStringMap;
import org.gephi.graph.api.types.TimeMap;
import org.gephi.graph.api.types.TimeSet;
import org.gephi.graph.api.types.TimestampBooleanMap;
import org.gephi.graph.api.types.TimestampByteMap;
import org.gephi.graph.api.types.TimestampCharMap;
import org.gephi.graph.api.types.TimestampDoubleMap;
import org.gephi.graph.api.types.TimestampFloatMap;
import org.gephi.graph.api.types.TimestampIntegerMap;
import org.gephi.graph.api.types.TimestampLongMap;
import org.gephi.graph.api.types.TimestampMap;
import org.gephi.graph.api.types.TimestampSet;
import org.gephi.graph.api.types.TimestampShortMap;
import org.gephi.graph.api.types.TimestampStringMap;
import org.gephi.graph.impl.ArraysParser;
import org.gephi.graph.impl.FormattingAndParsingUtils;
import org.gephi.graph.impl.GraphStoreConfiguration;
import org.gephi.graph.impl.IntervalsParser;
import org.gephi.graph.impl.TimestampsParser;

/**
* Set of utility methods to manipulate supported attribute types.
Expand Down Expand Up @@ -1215,4 +1216,148 @@ public static boolean isNodeColumn(Column colum) {
public static boolean isEdgeColumn(Column colum) {
return colum.getTable().getElementClass().equals(Edge.class);
}

/**
* Returns a copy of the provided object.
* <p>
* The copy is a deep copy for arrays, {@link IntervalSet},
* {@link TimestampSet}, sets and lists
*
* @param obj object to copy
* @return copy of the provided object
*/
public static Object copy(Object obj) {
if (obj == null) {
return null;
}
Class typeClass = obj.getClass();
if (!isSupported(typeClass)) {
throw new IllegalArgumentException("Unsupported type " + typeClass.getCanonicalName());
}
typeClass = getStandardizedType(typeClass);
obj = standardizeValue(obj);

// Primitive
if (isSimpleType(typeClass)) {
return obj;
}

// Interval types:
if (typeClass.equals(IntervalSet.class)) {
return new IntervalSet((IntervalSet) obj);
} else if (typeClass.equals(IntervalStringMap.class)) {
return new IntervalStringMap((IntervalStringMap) obj);
} else if (typeClass.equals(IntervalByteMap.class)) {
return new IntervalByteMap((IntervalByteMap) obj);
} else if (typeClass.equals(IntervalShortMap.class)) {
return new IntervalShortMap((IntervalShortMap) obj);
} else if (typeClass.equals(IntervalIntegerMap.class)) {
return new IntervalIntegerMap((IntervalIntegerMap) obj);
} else if (typeClass.equals(IntervalLongMap.class)) {
return new IntervalLongMap((IntervalLongMap) obj);
} else if (typeClass.equals(IntervalFloatMap.class)) {
return new IntervalFloatMap((IntervalFloatMap) obj);
} else if (typeClass.equals(IntervalDoubleMap.class)) {
return new IntervalDoubleMap((IntervalDoubleMap) obj);
} else if (typeClass.equals(IntervalBooleanMap.class)) {
return new IntervalBooleanMap((IntervalBooleanMap) obj);
} else if (typeClass.equals(IntervalCharMap.class)) {
return new IntervalCharMap((IntervalCharMap) obj);
}

// Timestamp types:
if (typeClass.equals(TimestampSet.class)) {
return new TimestampSet((TimestampSet) obj);
} else if (typeClass.equals(TimestampStringMap.class)) {
return new TimestampStringMap((TimestampStringMap) obj);
} else if (typeClass.equals(TimestampByteMap.class)) {
return new TimestampByteMap((TimestampByteMap) obj);
} else if (typeClass.equals(TimestampShortMap.class)) {
return new TimestampShortMap((TimestampShortMap) obj);
} else if (typeClass.equals(TimestampIntegerMap.class)) {
return new TimestampIntegerMap((TimestampIntegerMap) obj);
} else if (typeClass.equals(TimestampLongMap.class)) {
return new TimestampLongMap((TimestampLongMap) obj);
} else if (typeClass.equals(TimestampFloatMap.class)) {
return new TimestampFloatMap((TimestampFloatMap) obj);
} else if (typeClass.equals(TimestampDoubleMap.class)) {
return new TimestampDoubleMap((TimestampDoubleMap) obj);
} else if (typeClass.equals(TimestampBooleanMap.class)) {
return new TimestampBooleanMap((TimestampBooleanMap) obj);
} else if (typeClass.equals(TimestampCharMap.class)) {
return new TimestampCharMap((TimestampCharMap) obj);
}

// Array
if (isArrayType(typeClass)) {
Class componentType = typeClass.getComponentType();
int length = Array.getLength(obj);
Object dest = Array.newInstance(componentType, length);
System.arraycopy(obj, 0, dest, 0, length);
return dest;
}

// List
if (obj instanceof CharArrayList) {
return new CharArrayList((CharArrayList) obj);
} else if (obj instanceof BooleanArrayList) {
return new BooleanArrayList((BooleanArrayList) obj);
} else if (obj instanceof ByteArrayList) {
return new ByteArrayList((ByteArrayList) obj);
} else if (obj instanceof ShortArrayList) {
return new ShortArrayList((ShortArrayList) obj);
} else if (obj instanceof IntArrayList) {
return new IntArrayList((IntArrayList) obj);
} else if (obj instanceof LongArrayList) {
return new LongArrayList((LongArrayList) obj);
} else if (obj instanceof FloatArrayList) {
return new FloatArrayList((FloatArrayList) obj);
} else if (obj instanceof DoubleArrayList) {
return new DoubleArrayList((DoubleArrayList) obj);
} else if (obj instanceof ObjectArrayList) {
return new ObjectArrayList((ObjectArrayList) obj);
}

// Map
if (obj instanceof Char2ObjectOpenHashMap) {
return new Char2ObjectOpenHashMap((Char2ObjectOpenHashMap) obj);
} else if (obj instanceof Byte2ObjectOpenHashMap) {
return new Byte2ObjectOpenHashMap((Byte2ObjectOpenHashMap) obj);
} else if (obj instanceof Short2ObjectOpenHashMap) {
return new Short2ObjectOpenHashMap((Short2ObjectOpenHashMap) obj);
} else if (obj instanceof Int2ObjectOpenHashMap) {
return new Int2ObjectOpenHashMap((Int2ObjectOpenHashMap) obj);
} else if (obj instanceof Long2ObjectOpenHashMap) {
return new Long2ObjectOpenHashMap((Long2ObjectOpenHashMap) obj);
} else if (obj instanceof Float2ObjectOpenHashMap) {
return new Float2ObjectOpenHashMap((Float2ObjectOpenHashMap) obj);
} else if (obj instanceof Double2ObjectOpenHashMap) {
return new Double2ObjectOpenHashMap((Double2ObjectOpenHashMap) obj);
} else if (obj instanceof Object2ObjectOpenHashMap) {
return new Object2ObjectOpenHashMap((Object2ObjectOpenHashMap) obj);
}

// Set
if (obj instanceof CharOpenHashSet) {
return new CharOpenHashSet((CharOpenHashSet) obj);
} else if (obj instanceof BooleanOpenHashSet) {
return new BooleanOpenHashSet((BooleanOpenHashSet) obj);
} else if (obj instanceof ByteOpenHashSet) {
return new ByteOpenHashSet((ByteOpenHashSet) obj);
} else if (obj instanceof ShortOpenHashSet) {
return new ShortOpenHashSet((ShortOpenHashSet) obj);
} else if (obj instanceof IntOpenHashSet) {
return new IntOpenHashSet((IntOpenHashSet) obj);
} else if (obj instanceof LongOpenHashSet) {
return new LongOpenHashSet((LongOpenHashSet) obj);
} else if (obj instanceof FloatOpenHashSet) {
return new FloatOpenHashSet((FloatOpenHashSet) obj);
} else if (obj instanceof DoubleOpenHashSet) {
return new DoubleOpenHashSet((DoubleOpenHashSet) obj);
} else if (obj instanceof ObjectOpenHashSet) {
return new ObjectOpenHashSet((ObjectOpenHashSet) obj);
}

return obj;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/gephi/graph/api/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* create a <em>GraphModel</em> with custom configuration.
* <p>
* Create instances by using the builder:
*
* <pre>
* Configuration config = Configuration.builder().build();
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public IntervalBooleanMap(double[] keys, boolean[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalBooleanMap(IntervalBooleanMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/gephi/graph/api/types/IntervalByteMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public IntervalByteMap(double[] keys, byte[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalByteMap(IntervalByteMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/gephi/graph/api/types/IntervalCharMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public IntervalCharMap(double[] keys, char[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalCharMap(IntervalCharMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public IntervalDoubleMap(double[] keys, double[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalDoubleMap(IntervalDoubleMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/gephi/graph/api/types/IntervalFloatMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public IntervalFloatMap(double[] keys, float[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalFloatMap(IntervalFloatMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public IntervalIntegerMap(double[] keys, int[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalIntegerMap(IntervalIntegerMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/gephi/graph/api/types/IntervalLongMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public IntervalLongMap(double[] keys, long[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalLongMap(IntervalLongMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/gephi/graph/api/types/IntervalSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public IntervalSet(double[] arr) {
size = arr.length / 2;
}

/**
* Copy constructor.
*
* @param source set to copy
*/
public IntervalSet(IntervalSet source) {
this(source.array);
}

@Override
public boolean add(Interval interval) {
return addInner(interval.getLow(), interval.getHigh()) >= 0;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/gephi/graph/api/types/IntervalShortMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public IntervalShortMap(double[] keys, short[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalShortMap(IntervalShortMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given interval.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public IntervalStringMap(double[] keys, String[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public IntervalStringMap(IntervalStringMap source) {
this(source.array, source.values);
}

@Override
public Class<String> getTypeClass() {
return String.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public TimestampBooleanMap(double[] keys, boolean[] vals) {
System.arraycopy(vals, 0, values, 0, vals.length);
}

/**
* Copy constructor.
*
* @param source the map to copy
*/
public TimestampBooleanMap(TimestampBooleanMap source) {
this(source.array, source.values);
}

/**
* Get the value for the given timestamp.
*
Expand Down
Loading