Skip to content

Commit

Permalink
Add OperatorImpl cache population #481
Browse files Browse the repository at this point in the history
  • Loading branch information
timowest committed Aug 23, 2013
1 parent 25845de commit 1b5e319
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
@@ -1,6 +1,6 @@
/*
* Copyright 2011, Mysema Ltd
*
*
* 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
Expand Down Expand Up @@ -32,7 +32,7 @@
*/
@Immutable
public class OperationImpl<T> extends ExpressionBase<T> implements Operation<T> {

private static final long serialVersionUID = 4796432056083507588L;

private final ImmutableList<Expression<?>> args;
Expand All @@ -42,15 +42,15 @@ public class OperationImpl<T> extends ExpressionBase<T> implements Operation<T>
public static <RT> Operation<RT> create(Class<? extends RT> type, Operator<? super RT> operator, Expression<?> one) {
return new OperationImpl<RT>(type, operator, ImmutableList.<Expression<?>>of(one));
}

public static <RT> Operation<RT> create(Class<? extends RT> type, Operator<? super RT> operator, Expression<?> one, Expression<?> two) {
return new OperationImpl<RT>(type, operator, ImmutableList.of(one, two));
}

protected OperationImpl(Class<? extends T> type, Operator<? super T> operator, Expression<?>... args) {
this(type, operator, ImmutableList.copyOf(args));
}

public OperationImpl(Class<? extends T> type, Operator<? super T> operator, ImmutableList<Expression<?>> args) {
super(type);
this.operator = operator;
Expand Down Expand Up @@ -90,23 +90,23 @@ public final boolean equals(Object o) {
public final <R, C> R accept(Visitor<R, C> v, C context) {
return v.visit(this, context);
}

/**
* Resets operator field to singleton version
*
*
* @param ois
* @throws ClassNotFoundException
* @throws IOException
*/
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
try {
ois.defaultReadObject();
Field field = OperationImpl.class.getDeclaredField("operator");
field.setAccessible(true);
field.set(this, OperatorImpl.OPS.get(operator.getId()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

}
@@ -1,6 +1,6 @@
/*
* Copyright 2011, Mysema Ltd
*
*
* 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
Expand All @@ -13,7 +13,11 @@
*/
package com.mysema.query.types;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.concurrent.Immutable;
Expand All @@ -25,13 +29,29 @@
public final class OperatorImpl<T> implements Operator<T> {

static final Map<String, Operator<?>> OPS = new HashMap<String, Operator<?>>(150);


static {
try {
// initialize all fields of Ops
List<Field> fields = new ArrayList<Field>();
fields.addAll(Arrays.asList(Ops.class.getFields()));
for (Class<?> cl : Ops.class.getClasses()) {
fields.addAll(Arrays.asList(cl.getFields()));
}
for (Field field : fields) {
field.get(null);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}

private static final long serialVersionUID = -2435035383548549877L;

private final String id;

private final int hashCode;

public OperatorImpl(String id) {
this.id = id;
this.hashCode = id.hashCode();
Expand All @@ -43,15 +63,16 @@ public OperatorImpl(String id) {
*
* @return
*/
@Override
public String getId() {
return id;
}

@Override
public String toString() {
return id;
}

@Override
public int hashCode() {
return hashCode;
Expand Down
@@ -0,0 +1,19 @@
package com.mysema.query.types;

import static org.junit.Assert.assertFalse;

import java.lang.reflect.Field;
import java.util.Map;

import org.junit.Test;

public class OperatorImplTest {

@Test
public void Cache_Isnt_Empty() throws Exception {
Field field = OperatorImpl.class.getDeclaredField("OPS");
field.setAccessible(true);
Map map = (Map) field.get(null);
assertFalse(map.isEmpty());
}
}

0 comments on commit 1b5e319

Please sign in to comment.