Skip to content

Commit

Permalink
Replace calls to teiid utility classes
Browse files Browse the repository at this point in the history
* Replaces calls to EquivalenceUtil and ExternalizeUtil with designer
  classes
  • Loading branch information
Paul Richardson committed Nov 15, 2012
1 parent 817dd16 commit 29ad9d2
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.teiid.core.designer.TeiidDesignerException;
import org.teiid.core.util.EquivalenceUtil;
import org.teiid.core.util.ExternalizeUtil;
import org.teiid.core.designer.util.EquivalenceUtil;
import org.teiid.core.designer.util.ExternalizeUtil;

/**
* @since 8.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/

package org.teiid.core.designer.util;

import java.util.Arrays;

/**
* Utilities to test the equivalence (see method-specific definitions) of any
* two object/array references.
*
* @since 8.0
*/
public class EquivalenceUtil {

/**
* Cannot be instantiated
*/
protected EquivalenceUtil() {
}

/**
* Tests whether two object references refer to equal objects. The object
* references can both be null, in which case they are also considered equal.
* @param obj1 object reference
* @param obj2 object reference
* @return true if both references are null, OR if neither is null and both
* objects are equal; false otherwise
*/
public static boolean areEqual(Object obj1, Object obj2) {
if (obj1 == obj2) {
return true;
} else if (obj1 == null || obj2 == null) {
return false;
} else {
return obj1.equals(obj2);
}
}

/**
* Tests whether two arrays are equivalent. This method ignores the array
* types, but checks the number of references in each array, and the
* equivalence of those references (in ascending index order).
* @param array1 an object array
* @param array2 an object array
*/
public static boolean areEquivalent(Object[] array1, Object[] array2) {
return Arrays.equals(array1, array2);
}

/**
* Tests whether two objects references are equivalent but not the same object.
* If both references are the same, this method will return false;
* @param obj1 object reference
* @param obj2 object reference
* @return true if the two references are unequal, and the objects they
* refer to are equal
*/
public static boolean areStrictlyEquivalent(Object obj1, Object obj2) {
if (obj1 == obj2 || obj1 == null || obj2 == null) {
return false;
}
return areEqual(obj1, obj2);
}

/**
* Tests whether the array references are equivalent, but are not the same.
* This method checks for the strict equivalence of each pair of objects
* in the two arrays (i.e. corresponding objects cannot be the same but must
* be equivalent) in ascending index order. This method also considers a
* null array and a 0-length array equivalent, and ignores the array type.
* @param array1 an object array
* @param array2 an object array
* @return true if the arrays are equivalent, but not the same.
*/
public static boolean areStrictlyEquivalent(Object[] array1, Object[] array2) {
if (array1 == array2) {
return false;
} else if (array1 == null) {
return array2.length == 0;
} else if (array2 == null) {
return array1.length == 0;
} else if (array1.length != array2.length) {
return false;
} else {
for (int i = 0; i < array1.length; i++) {
if (!areStrictlyEquivalent(array1[i], array2[i])) {
return false;
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/

package org.teiid.core.designer.util;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Utilities used by Externalizable classes to read/write objects from
* ObjectInput/ObjectOutput instances.
*
* @since 8.0
*/
public class ExternalizeUtil {

private ExternalizeUtil() {
}

/**
* Writes an array to the output.
* @param out the output instance
* @param array reference to an array. Can be null.
* @throws IOException
*/
public static void writeArray(ObjectOutput out, Object[] array) throws IOException {
if (array == null) {
out.writeInt(0);
} else {
final int length = array.length;
out.writeInt(length);
for (int i = 0; i < length; i++) {
out.writeObject(array[i]);
}
}
}

/**
* Writes a Collection to the output using its Iterator.
* @param out the output instance
* @param coll reference to a Collection. Can be null.
* @throws IOException
*/
public static void writeCollection(ObjectOutput out, Collection<?> coll) throws IOException {
if (coll == null) {
out.writeInt(0);
} else {
final int size = coll.size();
out.writeInt(coll.size());
if (size > 0) {
for (Object object : coll) {
out.writeObject(object);
}
}
}
}

public static void writeList(ObjectOutput out, List<?> coll) throws IOException {
writeCollection(out, coll);
}

/**
* Writes the key-value pairs of the given map to the output.
* @param out the output instance
* @param list reference to a Map. Can be null.
* @throws IOException
*/
public static void writeMap(ObjectOutput out, Map<?, ?> map) throws IOException {
if (map == null) {
out.writeInt(0);
} else {
out.writeInt(map.size());
for (Map.Entry<?, ?> entry : map.entrySet()) {
out.writeObject(entry.getKey());
out.writeObject(entry.getValue());
}
}
}

/**
* Reads an array of String that was written to the output by this utility class
* @param in
* @return a non-null String[]
* @throws IOException
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
public static <T> T[] readArray(ObjectInput in, Class<T> type) throws IOException, ClassNotFoundException {
final int length = in.readInt();
T[] result = (T[])Array.newInstance(type, length);
for (int i = 0; i < length; i++) {
result[i] = type.cast(in.readObject());
}
return result;
}

public static String[] readStringArray(ObjectInput in) throws IOException, ClassNotFoundException {
return readArray(in, String.class);
}

/**
* Reads a List that was written by this utility class.
* @param in
* @return a non-null List
* @throws IOException
* @throws ClassNotFoundException
*/
public static <T> List<T> readList(ObjectInput in, Class<T> type) throws IOException, ClassNotFoundException {
return Arrays.asList(readArray(in, type));
}

public static List<?> readList(ObjectInput in) throws IOException, ClassNotFoundException {
return readList(in, Object.class);
}

/**
* Reads a Map that was written by this utility class
* @param in
* @return a non-null Map
* @throws IOException
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> readMap(ObjectInput in) throws IOException, ClassNotFoundException {
final int size = in.readInt();
HashMap<K, V> map = new HashMap<K, V>(size);
for (int i = 0; i < size; i++) {
map.put((K)in.readObject(), (V)in.readObject());
}
return map;
}

public static void writeEnum(ObjectOutput out, Enum<?> value) throws IOException {
if (value == null) {
out.writeObject(null);
} else {
out.writeUTF(value.name());
}
}

public static <T extends Enum<T>> T readEnum(ObjectInput in, Class<T> clazz) throws IOException {
String name = in.readUTF();
if (name == null) {
return null;
}
return Enum.valueOf(clazz, name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.core.designer.util.FileUtils;

import org.teiid.core.util.EquivalenceUtil;
import org.teiid.core.designer.util.EquivalenceUtil;
import org.teiid.core.designer.HashCodeUtil;
import org.teiid.designer.core.container.EObjectFinder;
import org.teiid.designer.core.index.IndexConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import java.io.File;
import java.io.InputStream;
import org.teiid.core.util.EquivalenceUtil;
import org.teiid.core.designer.HashCodeUtil;
import org.teiid.core.designer.util.EquivalenceUtil;
import org.teiid.core.designer.util.FileUtils;
import org.teiid.designer.core.index.IndexConstants;
import org.teiid.designer.core.index.IndexSelector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.teiid.core.util.EquivalenceUtil;
import org.teiid.core.designer.util.EquivalenceUtil;
import org.teiid.core.util.SmartTestDesignerSuite;
import org.teiid.designer.ui.common.dialog.FileUiUtils;

Expand Down

0 comments on commit 29ad9d2

Please sign in to comment.