add helper methods to field data

This commit is contained in:
kimchy 2010-06-04 06:15:35 +03:00
parent 13d7f10b7b
commit 6e15e3dd24
8 changed files with 70 additions and 7 deletions

View File

@ -88,6 +88,13 @@ public abstract class FieldData {
public abstract String stringValue(int docId);
public abstract void forEachValue(StringValueProc proc);
public static interface StringValueProc {
void onValue(String value, int freq);
}
/**
* The type of this field data.
*/

View File

@ -52,6 +52,18 @@ public abstract class DoubleFieldData extends NumericFieldData {
return Double.toString(value(docId));
}
@Override public void forEachValue(StringValueProc proc) {
if (freqs == null) {
for (int i = 1; i < values.length; i++) {
proc.onValue(Double.toString(values[i]), -1);
}
} else {
for (int i = 1; i < values.length; i++) {
proc.onValue(Double.toString(values[i]), freqs[i]);
}
}
}
@Override public byte byteValue(int docId) {
return (byte) value(docId);
}

View File

@ -52,6 +52,18 @@ public abstract class FloatFieldData extends NumericFieldData {
return Float.toString(value(docId));
}
@Override public void forEachValue(StringValueProc proc) {
if (freqs == null) {
for (int i = 1; i < values.length; i++) {
proc.onValue(Float.toString(values[i]), -1);
}
} else {
for (int i = 1; i < values.length; i++) {
proc.onValue(Float.toString(values[i]), freqs[i]);
}
}
}
@Override public byte byteValue(int docId) {
return (byte) value(docId);
}

View File

@ -52,6 +52,18 @@ public abstract class IntFieldData extends NumericFieldData {
return Integer.toString(value(docId));
}
@Override public void forEachValue(StringValueProc proc) {
if (freqs == null) {
for (int i = 1; i < values.length; i++) {
proc.onValue(Integer.toString(values[i]), -1);
}
} else {
for (int i = 1; i < values.length; i++) {
proc.onValue(Integer.toString(values[i]), freqs[i]);
}
}
}
@Override public byte byteValue(int docId) {
return (byte) value(docId);
}

View File

@ -49,6 +49,18 @@ public abstract class LongFieldData extends NumericFieldData {
abstract public long[] values(int docId);
@Override public void forEachValue(StringValueProc proc) {
if (freqs == null) {
for (int i = 1; i < values.length; i++) {
proc.onValue(Long.toString(values[i]), -1);
}
} else {
for (int i = 1; i < values.length; i++) {
proc.onValue(Long.toString(values[i]), freqs[i]);
}
}
}
@Override public String stringValue(int docId) {
return Long.toString(docId);
}

View File

@ -48,6 +48,18 @@ public abstract class ShortFieldData extends NumericFieldData {
abstract public short[] values(int docId);
@Override public void forEachValue(StringValueProc proc) {
if (freqs == null) {
for (int i = 1; i < values.length; i++) {
proc.onValue(Short.toString(values[i]), -1);
}
} else {
for (int i = 1; i < values.length; i++) {
proc.onValue(Short.toString(values[i]), freqs[i]);
}
}
}
@Override public String stringValue(int docId) {
return Short.toString(value(docId));
}

View File

@ -53,7 +53,7 @@ public abstract class StringFieldData extends FieldData {
return Type.STRING;
}
public void forEachValue(ValueProc proc) {
@Override public void forEachValue(StringValueProc proc) {
if (freqs == null) {
for (int i = 1; i < values.length; i++) {
proc.onValue(values[i], -1);
@ -65,10 +65,6 @@ public abstract class StringFieldData extends FieldData {
}
}
public static interface ValueProc {
void onValue(String value, int freq);
}
public static StringFieldData load(IndexReader reader, String field, FieldDataOptions options) throws IOException {
return FieldDataLoader.load(reader, field, options, new StringTypeLoader());
}

View File

@ -103,7 +103,7 @@ public class StringFieldDataTests {
// check order is correct
final ArrayList<Tuple<String, Integer>> values = new ArrayList<Tuple<String, Integer>>();
sFieldData.forEachValue(new StringFieldData.ValueProc() {
sFieldData.forEachValue(new FieldData.StringValueProc() {
@Override public void onValue(String value, int freq) {
values.add(tuple(value, freq));
}
@ -142,7 +142,7 @@ public class StringFieldDataTests {
assertThat(mFieldData.hasValue(4), equalTo(false));
values.clear();
mFieldData.forEachValue(new StringFieldData.ValueProc() {
mFieldData.forEachValue(new FieldData.StringValueProc() {
@Override public void onValue(String value, int freq) {
values.add(tuple(value, freq));
}