Skip to content

Commit

Permalink
Slight refactoring to file sort code and tweaked the FramedElement to…
Browse files Browse the repository at this point in the history
… cache lookups
  • Loading branch information
jsight committed Sep 16, 2015
1 parent 668348d commit c2010b6
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 65 deletions.
140 changes: 114 additions & 26 deletions forks/frames/src/main/java/com/tinkerpop/frames/FramedElement.java
@@ -1,16 +1,18 @@
package com.tinkerpop.frames;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.util.ElementHelper;
import com.tinkerpop.frames.annotations.AnnotationHandler;
import com.tinkerpop.frames.modules.MethodHandler;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

/**
* The proxy class of a framed element.
*
Expand All @@ -26,7 +28,8 @@ public class FramedElement implements InvocationHandler {
private static Method toStringMethod;
private static Method asVertexMethod;
private static Method asEdgeMethod;

private static Map<MethodCallEntry, MethodHandlerEntry> methocCallCache = Collections
.synchronizedMap(new HashMap<MethodCallEntry, MethodHandlerEntry>());

static {
try {
Expand Down Expand Up @@ -62,44 +65,71 @@ public FramedElement(final FramedGraph framedGraph, final Element element) {
}

public Object invoke(final Object proxy, final Method originalMethod, final Object[] arguments) {
MethodCallEntry methodCallEntry = new MethodCallEntry(proxy.getClass(), originalMethod);

MethodHandlerEntry methodHandlerEntry = methocCallCache.get(methodCallEntry);
if (methodHandlerEntry != null)
{
// hitCount.incrementAndGet();
if (methodHandlerEntry.methodHandler != null)
return methodHandlerEntry.methodHandler.processElement(proxy, methodHandlerEntry.method, arguments, methodHandlerEntry.annotation,
this.framedGraph,
this.element);
else if (methodHandlerEntry.annotationHandler != null)
return methodHandlerEntry.annotationHandler.processElement(methodHandlerEntry.annotation, methodHandlerEntry.method, arguments,
this.framedGraph, this.element,
this.direction);
}
Method method = null;
Class<?> methodInterface = null;

// try to find the method on one of the proxy's interfaces
// (the passed in Method is often from a superclass or from the {@link Proxy} object itself,
// so we need to make sure we find the method that the user actually intended)
for (Class<?> c : proxy.getClass().getInterfaces()) {
if (method != null && c.isAssignableFrom(methodInterface)) {
// don't search this class if we already have found a method from a subclass of it
continue;
// so we need to make sure we find the method that the user actually intended)
for (Class<?> c : proxy.getClass().getInterfaces())
{
if (method != null && c.isAssignableFrom(methodInterface))
{
// don't search this class if we already have found a method from a subclass of it
continue;
}

for (Method interfaceMethod : c.getMethods()) {
if (compareMethods(originalMethod, interfaceMethod)) {
if (interfaceMethod.getAnnotations().length > 0) {
method = interfaceMethod;
methodInterface = c;

for (Method interfaceMethod : c.getMethods())
{
if (compareMethods(originalMethod, interfaceMethod))
{
if (interfaceMethod.getAnnotations().length > 0)
{
method = interfaceMethod;
methodInterface = c;
}
break;
}
}
}
if (method == null) {
if (method == null)
{
method = originalMethod;
}

Annotation[] annotations = method.getAnnotations();
Map<Class<? extends Annotation>, AnnotationHandler<?>> annotationHandlers = this.framedGraph.getConfig().getAnnotationHandlers();
Map<Class<? extends Annotation>, MethodHandler<?>> methodHandlers = this.framedGraph.getConfig().getMethodHandlers();
for (final Annotation annotation : annotations) {
MethodHandler methodHandler = methodHandlers.get(annotation.annotationType());
if (methodHandler != null) {
for (final Annotation annotation : annotations)
{
MethodHandler methodHandler = methodHandlers.get(annotation.annotationType());
if (methodHandler != null)
{
methocCallCache.put(methodCallEntry, new MethodHandlerEntry(method, annotation, methodHandler));
return methodHandler.processElement(proxy, method, arguments, annotation, this.framedGraph, this.element);
}
}
for (final Annotation annotation : annotations) {
AnnotationHandler annotationHandler = annotationHandlers.get(annotation.annotationType());
if (annotationHandler != null) {
for (final Annotation annotation : annotations)
{
AnnotationHandler annotationHandler = annotationHandlers.get(annotation.annotationType());
if (annotationHandler != null)
{
methocCallCache.put(methodCallEntry, new MethodHandlerEntry(method, annotation, annotationHandler));
return annotationHandler.processElement(annotation, method, arguments, this.framedGraph, this.element, this.direction);
}
}
Expand Down Expand Up @@ -160,4 +190,62 @@ private Boolean proxyEquals(final Object other) {
public Element getElement() {
return this.element;
}

private class MethodCallEntry
{
private Class clazz;
private Method method;

public MethodCallEntry(Class clazz, Method method)
{
this.clazz = clazz;
this.method = method;
}

@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

MethodCallEntry that = (MethodCallEntry) o;

if (clazz != null ? !clazz.equals(that.clazz) : that.clazz != null)
return false;
return !(method != null ? !method.equals(that.method) : that.method != null);

}

@Override
public int hashCode()
{
int result = clazz != null ? clazz.hashCode() : 0;
result = 31 * result + (method != null ? method.hashCode() : 0);
return result;
}
}

private class MethodHandlerEntry
{
private final Method method;
private final Annotation annotation;
private MethodHandler methodHandler;
private AnnotationHandler annotationHandler;

public MethodHandlerEntry(Method method, Annotation annotation, MethodHandler<?> methodHandler)
{
this.method = method;
this.annotation = annotation;
this.methodHandler = methodHandler;
}

public MethodHandlerEntry(Method method, Annotation annotation, AnnotationHandler<?> annotationHandler)
{
this.method = method;
this.annotation = annotation;
this.annotationHandler = annotationHandler;
}
}
}
Expand Up @@ -21,8 +21,6 @@ public Class<JavaHandler> getAnnotationType() {
return JavaHandler.class;
}



@Override
public Object processElement(Object framedElement, Method method,
Object[] arguments, JavaHandler annotation,
Expand Down
Expand Up @@ -4,6 +4,8 @@
import java.nio.file.Paths;
import java.util.Comparator;

import org.jboss.windup.util.ExecutionStatistics;

/**
* Sorts file paths according to their alphabetical order.
*
Expand All @@ -30,39 +32,47 @@ public class FilePathComparator implements Comparator<String>
@Override
public int compare(String o1, String o2)
{
// if they are exactly the same, just short circuit everything
// and return 0
if (o1.equals(o2))
ExecutionStatistics.get().begin("FilePathComparator");
try
{
return 0;
}
// if they are exactly the same, just short circuit everything
// and return 0
if (o1.equals(o2))
{
return 0;
}

// split by the path separator (/ or \)
Path path1 = Paths.get(o1);
Path path2 = Paths.get(o2);
// split by the path separator (/ or \)
Path path1 = Paths.get(o1);
Path path2 = Paths.get(o2);

if (path1.getNameCount() != path2.getNameCount())
{
// if there are differing number of path elements, compare based on number of segments
return path1.getNameCount() - path2.getNameCount();
}
else
{
// otherwise, compare each segment
for (int i = 0; i < path1.getNameCount(); i++)
if (path1.getNameCount() != path2.getNameCount())
{
String o1Segment = path1.getName(i).toString();
String o2Segment = path2.getName(i).toString();

// if the segments are different, return the results of this comparison
if (!o1Segment.equals(o2Segment))
// if there are differing number of path elements, compare based on number of segments
return path1.getNameCount() - path2.getNameCount();
}
else
{
// otherwise, compare each segment
for (int i = 0; i < path1.getNameCount(); i++)
{
return o1Segment.compareTo(o2Segment);
String o1Segment = path1.getName(i).toString();
String o2Segment = path2.getName(i).toString();

// if the segments are different, return the results of this comparison
if (!o1Segment.equals(o2Segment))
{
return o1Segment.compareTo(o2Segment);
}
}
}

// no segments differed, so just return 0 (same path)
return 0;
// no segments differed, so just return 0 (same path)
return 0;
}
}
finally
{
ExecutionStatistics.get().end("FilePathComparator");
}
}
}
@@ -1,9 +1,9 @@
package org.jboss.windup.reporting.freemarker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.graph.model.comparator.FilePathComparator;
Expand Down Expand Up @@ -62,24 +62,26 @@ public Object exec(@SuppressWarnings("rawtypes") List arguments) throws Template
throw new TemplateModelException("Error, method expects one argument (Iterable<FileModel>)");
}
Iterable<FileModel> fileModelIterable = getList(arguments.get(0));
List<FileModel> fileModelList = new ArrayList<>();
for (FileModel fm : fileModelIterable)
{
fileModelList.add(fm);
}

final FilePathComparator filePathComparator = new FilePathComparator();
Collections.sort(fileModelList, new Comparator<FileModel>()
Comparator<FileModel> fileModelComparator = new Comparator<FileModel>()
{
final FilePathComparator filePathComparator = new FilePathComparator();

@Override
public int compare(FileModel o1, FileModel o2)
{
return filePathComparator.compare(o1.getFilePath(), o2.getFilePath());
}
});
};

SortedSet<FileModel> fileModelSet = new TreeSet<>(fileModelComparator);
for (FileModel fm : fileModelIterable)
{
fileModelSet.add(fm);
}

ExecutionStatistics.get().end(NAME);
return fileModelList;
return fileModelSet;
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit c2010b6

Please sign in to comment.