Skip to content

Commit

Permalink
Predicate that matches the opposite of its argument
Browse files Browse the repository at this point in the history
  • Loading branch information
robbywalker committed Jun 3, 2012
1 parent b89adfe commit c519b69
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/main/java/com/greplin/lucene/predicate/Predicates.java
Expand Up @@ -4,7 +4,11 @@

package com.greplin.lucene.predicate;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.util.Bits;

import java.io.IOException;

/**
* Common predicates.
Expand Down Expand Up @@ -46,4 +50,79 @@ public static BitsProvider or(final BitsProvider... bitsProviders) {
return result;
}


/**
* Creates a new predicate bits provider that matches documents
* not matched by the argument.
* @param bitsProvider the predicate to negate.
* @return a new predicate bits provider that matches documents
* not matched by the argument.
*/
public static BitsProvider not(final BitsProvider bitsProvider) {
return new InverseBitsProvider(bitsProvider);
}


/**
* BitsProvider that matches the opposite set of bits as its
* argument.
*/
private static final class InverseBitsProvider extends BitsProvider {

/**
* The original bits to negate.
*/
private final BitsProvider original;


/**
* Creates a BitsProvider that negates its argument.
* @param original the bits to negate.
*/
private InverseBitsProvider(final BitsProvider original) {
this.original = original;
}


@Override
public Bits get(final IndexReader reader) throws IOException {
return new InverseBits(this.original.get(reader));
}

}


/**
* Bits that matches the opposite set of bits as its argument.
*/
private static final class InverseBits implements Bits {

/**
* The original bits to negate.
*/
private final Bits original;


/**
* Creates Bits that negate the argument.
* @param original the bits to negate.
*/
private InverseBits(final Bits original) {
this.original = original;
}


@Override
public boolean get(final int index) {
return !this.original.get(index);
}


@Override
public int length() {
return this.original.length();
}

}

}

0 comments on commit c519b69

Please sign in to comment.