-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
556 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/main/java/com/cedarsoftware/util/ClassUtilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.cedarsoftware.util; | ||
|
||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
|
||
/** | ||
* Useful utilities for Class work. For example, call computeInheritanceDistance(source, destination) | ||
* to get the inheritance distance (number of super class steps to make it from source to destination. | ||
* It will return the distance as an integer. If there is no inheritance relationship between the two, | ||
* then -1 is returned. The primitives and primitive wrappers return 0 distance as if they are the | ||
* same class. | ||
* <p> | ||
* @author John DeRegnaucourt ([email protected]) | ||
* <br> | ||
* Copyright (c) Cedar Software LLC | ||
* <br><br> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <br><br> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <br><br> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
public class ClassUtilities | ||
{ | ||
private static final Set<Class<?>> prims = new HashSet<>(); | ||
static | ||
{ | ||
prims.add(Byte.class); | ||
prims.add(Short.class); | ||
prims.add(Integer.class); | ||
prims.add(Long.class); | ||
prims.add(Float.class); | ||
prims.add(Double.class); | ||
prims.add(Character.class); | ||
prims.add(Boolean.class); | ||
} | ||
|
||
/** | ||
* Computes the inheritance distance between two classes/interfaces/primitive types. | ||
* @param source The source class, interface, or primitive type. | ||
* @param destination The destination class, interface, or primitive type. | ||
* @return The number of steps from the source to the destination, or -1 if no path exists. | ||
*/ | ||
public static int computeInheritanceDistance(Class<?> source, Class<?> destination) { | ||
if (source == null || destination == null) { | ||
return -1; | ||
} | ||
if (source.equals(destination)) { | ||
return 0; | ||
} | ||
|
||
// Check for primitive types | ||
if (source.isPrimitive()) { | ||
if (destination.isPrimitive()) { | ||
// Not equal because source.equals(destination) already chceked. | ||
return -1; | ||
} | ||
if (!isPrimitive(destination)) { | ||
return -1; | ||
} | ||
return comparePrimitiveToWrapper(destination, source); | ||
} | ||
|
||
if (destination.isPrimitive()) { | ||
if (!isPrimitive(source)) { | ||
return -1; | ||
} | ||
return comparePrimitiveToWrapper(source, destination); | ||
} | ||
|
||
Queue<Class<?>> queue = new LinkedList<>(); | ||
Set<Class<?>> visited = new HashSet<>(); | ||
queue.add(source); | ||
visited.add(source); | ||
|
||
int distance = 0; | ||
|
||
while (!queue.isEmpty()) { | ||
int levelSize = queue.size(); | ||
distance++; | ||
|
||
for (int i = 0; i < levelSize; i++) { | ||
Class<?> current = queue.poll(); | ||
|
||
// Check superclass | ||
if (current.getSuperclass() != null) { | ||
if (current.getSuperclass().equals(destination)) { | ||
return distance; | ||
} | ||
if (!visited.contains(current.getSuperclass())) { | ||
queue.add(current.getSuperclass()); | ||
visited.add(current.getSuperclass()); | ||
} | ||
} | ||
|
||
// Check interfaces | ||
for (Class<?> interfaceClass : current.getInterfaces()) { | ||
if (interfaceClass.equals(destination)) { | ||
return distance; | ||
} | ||
if (!visited.contains(interfaceClass)) { | ||
queue.add(interfaceClass); | ||
visited.add(interfaceClass); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return -1; // No path found | ||
} | ||
|
||
/** | ||
* @param c Class to test | ||
* @return boolean true if the passed in class is a Java primitive, false otherwise. The Wrapper classes | ||
* Integer, Long, Boolean, etc. are considered primitives by this method. | ||
*/ | ||
public static boolean isPrimitive(Class<?> c) | ||
{ | ||
return c.isPrimitive() || prims.contains(c); | ||
} | ||
|
||
/** | ||
* Compare two primitives. | ||
* @return 0 if they are the same, -1 if not. Primitive wrapper classes are consider the same as primitive classes. | ||
*/ | ||
public static int comparePrimitiveToWrapper(Class<?> source, Class<?> destination) | ||
{ | ||
try | ||
{ | ||
return source.getField("TYPE").get(null).equals(destination) ? 0 : -1; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new RuntimeException("Error while attempting comparison of primitive types: " + source.getName() + " vs " + destination.getName(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,27 @@ | |
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
/** | ||
* Buffered, Pushback, Reader that does not use synchronization. Much faster than the JDK variants because | ||
* they use synchronization. Typically, this class is used with a separate instance per thread, so | ||
* synchronization is not needed. | ||
* <p> | ||
* @author John DeRegnaucourt ([email protected]) | ||
* <br> | ||
* Copyright (c) Cedar Software LLC | ||
* <br><br> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <br><br> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <br><br> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
public class FastReader extends Reader { | ||
private Reader in; | ||
private char[] buf; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,27 @@ | |
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
/** | ||
* Buffered Writer that does not use synchronization. Much faster than the JDK variants because | ||
* they use synchronization. Typically, this class is used with a separate instance per thread, so | ||
* synchronization is not needed. | ||
* <p> | ||
* @author John DeRegnaucourt ([email protected]) | ||
* <br> | ||
* Copyright (c) Cedar Software LLC | ||
* <br><br> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <br><br> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <br><br> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
public class FastWriter extends Writer { | ||
private static final int DEFAULT_BUFFER_SIZE = 8192; | ||
|
||
|
Oops, something went wrong.