arangodb-java-driver icon indicating copy to clipboard operation
arangodb-java-driver copied to clipboard

Static Inner Classes and Entity interface

Open beoliver opened this issue 7 years ago • 3 comments

Following on from #215 As @christian-lechner mentioned, "Entity classes can also have static inner classes"

https://github.com/arangodb/arangodb-java-driver/issues/215#issuecomment-407837632

For example the getStats method on the cursor object returns CursorEntity.Stats

https://github.com/arangodb/arangodb-java-driver/blob/6c9ec7bdf4ecdc8c28dd78a5887646623c27a0bf/src/main/java/com/arangodb/entity/CursorEntity.java#L116

As static inner classes do not have access to their outer class, it is currently not possible to tell if such a class is an Entity or entity like

As the Entity interface is only used so that the entity classes have a superclass that is not Object it would make sense to make the static inner classes also implement Entity

beoliver avatar Aug 17 '18 02:08 beoliver

As static inner classes do not have access to their outer class, it is currently not possible to tell if such a class is an Entity or entity like

Instances of static inner classes don't have access to the enclosing instance (because there is none) , but Class objects can check if they are (static) inner classes:

boolean isEntityLike(final Object potentialEntity) {
    if (potentialEntity instanceof Entity) {
        return true;
    }
    final Class<?> cls = potentialEntity.getClass().getDeclaringClass();
    return cls != null ? Entity.class.isAssignableFrom(cls) : false;
}

christian-lechner avatar Aug 17 '18 06:08 christian-lechner

@christian-lechner - ok, but this would still have to be recursive as it is possible to have nested inner classes. Perhaps it is easier (and faster) to use the original approach of checking against the package name?

	private final static String entityPkgName = "com.arangodb.entity";
	
	public static boolean isEntity(Object o) {
		if (o == null) {
			return false;
		}
		return entityPkgName.equals(o.getClass().getPackage().getName());
	}

This process of converting Entity objects into Clojure data strictures (maps and vectors) is called recursively on every method call - so performance is an issue.

From some very quick tests it seems that when compared to isInstance checks - the package approach is ~ 6x slower while your proposed approach is ~20x slower.

beoliver avatar Aug 17 '18 10:08 beoliver

It's clear that my implementation is slower than an instanceof, because I'm doing an instanceof plus the getDeclaringClass() thing. But I can't imagine that str.startsWith("com.arangodb.entity") is "6x slower" than an instanceof.

christian-lechner avatar Aug 17 '18 11:08 christian-lechner

Not relevant anymore, since https://github.com/arangodb/arangodb-java-driver/releases/tag/v7.0.0-ALPHA.1

rashtao avatar Jan 20 '23 10:01 rashtao

Entity interface has been removed in version 7.0.0. https://github.com/arangodb/arangodb-java-driver/releases/tag/v7.0.0-RC.4

rashtao avatar Mar 21 '23 14:03 rashtao

Closing as fixed in version 7.0.0.

rashtao avatar Apr 20 '23 12:04 rashtao