In Lucene
, I am using the TaxonomyReader
to read an index of a taxonomy stored in my disk. For a given category, I need to find all categories that are its children. However, in the Lucene's API I could find a method to retrieve the parent but not with the children. There is a method called getChildrenArrays()
that returns a ChildrenArrays object. As you can see, this class only has two methods:
在Lucene中,我使用TaxonomyReader来读取存储在磁盘中的分类法的索引。对于给定的类别,我需要找到其子类的所有类别。但是,在Lucene的API中,我可以找到一种方法来检索父级而不是子级。有一个名为getChildrenArrays()的方法返回一个ChildrenArrays对象。如您所见,此类只有两种方法:
getYoungestChildArray
getOlderSiblingArray
I want to implement Enumerator
using these two methods. Does someone know how to do it?
我想使用这两种方法实现Enumerator。有人知道怎么做吗?
1 个解决方案
#1
0
I got the following:
我得到以下内容:
private class EnumChildren implements Enumeration<Integer> {
int category, current;
int[] next, first;
public EnumChildren(int category) {
ChildrenArrays childrenArrays = tr.getChildrenArrays();
first = childrenArrays.getYoungestChildArray();
next = childrenArrays.getOlderSiblingArray();
current = first[category];
}
public boolean hasChildren() {
return (current != TaxonomyReader.INVALID_ORDINAL);
}
@Override
public boolean hasMoreElements() {
current = next[current];
return (current != TaxonomyReader.INVALID_ORDINAL);
}
@Override
public Integer nextElement() {
return current;
}
}
Ans is used as:
Ans用作:
ordinal = tr.getOrdinal(new CategoryPath(nodeCategory.getPath(), '/'));
EnumChildren childrenEnumeration = new EnumChildren(ordinal);
if (childrenEnumeration.hasChildren()) {
do {
int current = childrenEnumeration.nextElement();
Category child = new Category(tr.getPath(current).toString());
addChildren(child);
nodeCategory.children.add(child);
} while (childrenEnumeration.hasMoreElements());
}
#1
0
I got the following:
我得到以下内容:
private class EnumChildren implements Enumeration<Integer> {
int category, current;
int[] next, first;
public EnumChildren(int category) {
ChildrenArrays childrenArrays = tr.getChildrenArrays();
first = childrenArrays.getYoungestChildArray();
next = childrenArrays.getOlderSiblingArray();
current = first[category];
}
public boolean hasChildren() {
return (current != TaxonomyReader.INVALID_ORDINAL);
}
@Override
public boolean hasMoreElements() {
current = next[current];
return (current != TaxonomyReader.INVALID_ORDINAL);
}
@Override
public Integer nextElement() {
return current;
}
}
Ans is used as:
Ans用作:
ordinal = tr.getOrdinal(new CategoryPath(nodeCategory.getPath(), '/'));
EnumChildren childrenEnumeration = new EnumChildren(ordinal);
if (childrenEnumeration.hasChildren()) {
do {
int current = childrenEnumeration.nextElement();
Category child = new Category(tr.getPath(current).toString());
addChildren(child);
nodeCategory.children.add(child);
} while (childrenEnumeration.hasMoreElements());
}