add helper methods to field data
This commit is contained in:
parent
13d7f10b7b
commit
6e15e3dd24
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue