Skip to content

Commit

Permalink
Add Stopwatch and negate to all PredicateType
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Oct 20, 2015
1 parent 1497ded commit 3aefe7a
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 25 deletions.
46 changes: 27 additions & 19 deletions src/main/java/com/speedment/field/predicate/PredicateType.java
Expand Up @@ -16,9 +16,9 @@
*/ */
package com.speedment.field.predicate; package com.speedment.field.predicate;


import java.util.Optional;

/** /**
* The PredicateType that exists for Speedment. A predicate type must always
* have a negated PredicateType that is the exact negation of itself.
* *
* @author pemi * @author pemi
*/ */
Expand All @@ -38,41 +38,49 @@ public enum PredicateType {
LESS_THAN, LESS_THAN,
LESS_OR_EQUAL, LESS_OR_EQUAL,
BETWEEN, BETWEEN,
//NOT_BETWEEN, // TO BE IMPLEMENTED NOT_BETWEEN, // Currently not exposed in external predicates
IN, IN,
NOT_IN, // Currently not exposed in external predicates
// String // String
EQUAL_IGNORE_CASE, EQUAL_IGNORE_CASE,
NOT_EQUAL_IGNORE_CASE, NOT_EQUAL_IGNORE_CASE,
STARTS_WITH, STARTS_WITH,
NOT_STARTS_WITH, // Currently not exposed in external predicates
ENDS_WITH, ENDS_WITH,
NOT_ENDS_WITH, // Currently not exposed in external predicates
CONTAINS, CONTAINS,
NOT_CONTAINS, // Currently not exposed in external predicates
IS_EMPTY, IS_EMPTY,
IS_NOT_EMPTY; IS_NOT_EMPTY;


private Optional<PredicateType> complementType; private PredicateType negatedType;


static { static {
associateComplimentTypes(ALWAYS_TRUE, ALWAYS_FALSE); associateNegations(ALWAYS_TRUE, ALWAYS_FALSE);
associateComplimentTypes(IS_NULL, IS_NOT_NULL); associateNegations(IS_NULL, IS_NOT_NULL);
associateComplimentTypes(EQUAL, NOT_EQUAL); associateNegations(EQUAL, NOT_EQUAL);
associateComplimentTypes(GREATER_THAN, LESS_OR_EQUAL); associateNegations(GREATER_THAN, LESS_OR_EQUAL);
associateComplimentTypes(GREATER_OR_EQUAL, LESS_THAN); associateNegations(GREATER_OR_EQUAL, LESS_THAN);
//associateComplimentTypes(BETWEEN, NOT_BETWEEN); associateNegations(BETWEEN, NOT_BETWEEN);
associateComplimentTypes(EQUAL_IGNORE_CASE, NOT_EQUAL_IGNORE_CASE); associateNegations(IN, NOT_IN);
associateComplimentTypes(IS_EMPTY, IS_NOT_EMPTY); associateNegations(EQUAL_IGNORE_CASE, NOT_EQUAL_IGNORE_CASE);
associateNegations(STARTS_WITH, NOT_STARTS_WITH);
associateNegations(ENDS_WITH, NOT_ENDS_WITH);
associateNegations(CONTAINS, NOT_CONTAINS);
associateNegations(IS_EMPTY, IS_NOT_EMPTY);
} }


public Optional<PredicateType> getComplementType() { public PredicateType negate() {
return complementType; return negatedType;
} }


public PredicateType effectiveType(boolean inverted) { public PredicateType effectiveType(boolean negate) {
return complementType.orElse(this); return negate ? negatedType : this;
} }


private static void associateComplimentTypes(PredicateType a, PredicateType b) { private static void associateNegations(PredicateType a, PredicateType b) {
a.complementType = Optional.of(b); a.negatedType = b;
b.complementType = Optional.of(a); b.negatedType = a;
} }


} }
Expand Up @@ -26,6 +26,12 @@
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;


/** /**
* *
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/speedment/internal/util/testing/Stopwatch.java
@@ -1,3 +1,19 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* 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:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.speedment.internal.util.testing; package com.speedment.internal.util.testing;


import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -73,11 +89,11 @@ public interface Stopwatch {
*/ */
Stopwatch reset(); Stopwatch reset();


default Stopwatch createStarted() { static Stopwatch createStarted() {
return create().start(); return create().start();
} }


default Stopwatch create() { static Stopwatch create() {
return new StopwatchImpl(); return new StopwatchImpl();
} }


Expand Down
@@ -1,3 +1,19 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* 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:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.speedment.internal.util.testing; package com.speedment.internal.util.testing;


import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -85,17 +101,17 @@ public String toString() {
final long nanos = elapsedNanos(); final long nanos = elapsedNanos();
final TimeUnit unit = timeUnitFor(nanos); final TimeUnit unit = timeUnitFor(nanos);
final double value = (double) nanos / NANOSECONDS.convert(1, unit); final double value = (double) nanos / NANOSECONDS.convert(1, unit);
return String.format("%.4g %s", value, unitShortText(unit)); return String.format("%,4g %s", value, unitShortText(unit));
} }


private static TimeUnit timeUnitFor(long nanos) { private static TimeUnit timeUnitFor(long nanos) {
if (SECONDS.convert(nanos, NANOSECONDS) > 10) { if (SECONDS.convert(nanos, NANOSECONDS) > 1) {
return SECONDS; return SECONDS;
} }
if (MILLISECONDS.convert(nanos, NANOSECONDS) > 10) { if (MILLISECONDS.convert(nanos, NANOSECONDS) > 1) {
return MILLISECONDS; return MILLISECONDS;
} }
if (MICROSECONDS.convert(nanos, NANOSECONDS) > 10) { if (MICROSECONDS.convert(nanos, NANOSECONDS) > 1) {
return MICROSECONDS; return MICROSECONDS;
} }
return NANOSECONDS; return NANOSECONDS;
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/com/speedment/field/predicate/PredicateTypeTest.java
@@ -0,0 +1,78 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.speedment.field.predicate;

import static com.speedment.field.predicate.PredicateType.EQUAL;
import static com.speedment.field.predicate.PredicateType.NOT_EQUAL;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.toList;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author pemi
*/
public class PredicateTypeTest {

public PredicateTypeTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

@Test
public void testComplementType() {
// Make sure that each Type has a (unique) negated type
final Map<PredicateType, PredicateType> map = new EnumMap(PredicateType.class);

for (PredicateType pt : PredicateType.values()) {
final PredicateType negated = pt.negate();
assertNotNull(negated);
assertFalse(map.containsKey(negated));
map.put(negated, pt);
}

final List<PredicateType> keys = map.keySet().stream().sorted().collect(toList());
final List<PredicateType> values = map.values().stream().sorted().collect(toList());
assertEquals(keys, values);

}

@Test
public void testGetComplementType() {
System.out.println("getComplementType");
assertEquals(EQUAL, NOT_EQUAL.negate());
assertEquals(NOT_EQUAL, EQUAL.negate());
}

@Test
public void testEffectiveType() {
System.out.println("effectiveType");
assertEquals(EQUAL, EQUAL.effectiveType(false));
assertEquals(NOT_EQUAL, EQUAL.effectiveType(true));
}

}

0 comments on commit 3aefe7a

Please sign in to comment.