2015-01-21 129 views
-1
interface Processable<E> 
{ 
    public E first(); 
    public E last(); 
    public List<E> processables(); 
    public E get(int index); 
} 

所以,我想要做的就是这 这里是一个什么样的查询应该做扩展和实现接口

final WidgetItem item = Inventory.select().identify("Bones").note().first(); 

class Query: ObjectQuery, Processable<E> 
{ 
    public E 
} 

}

一个例子,但有错误在 “E”

我试图做的,但在Java中

public abstract class Query<E, Q extends Query<E, Q>> implements Processable<E> { 

private final List<Filter<E>> filters; 

public Query() { 
    this.filters = new LinkedList<>(); 
} 

@SafeVarargs 
public final Q filter(final Filter<E>... filters) { 
    Collections.addAll(this.filters, filters); 
    return (Q) this; 
} 

protected final Filter<E> notThat(final Filter<E> filter) { 
    return new Filter<E>() { 
     @Override 
     public boolean accepts(final E e) { 
      return !filter.accepts(e); 
     } 
    }; 
} 

/** 
* @param e 
*   the element 
* @return true if all of the filters accepted the element 
*/ 
public final boolean accepts(final E e) { 
    for (final Filter<E> filter : filters) { 
     if (!filter.accepts(e)) 
      return false; 
    } 
    return true; 
} 

/** 
* @return a List of elements after filtering them all 
*/ 
public final List<E> all() { 
    final List<E> processables = processables(); 
    final ListIterator<E> iterator = processables.listIterator(); 
    while (iterator.hasNext()) { 
     if (!accepts(iterator.next())) 
      iterator.remove(); 
    } 
    return processables; 
} 

public final E first() { 
    final List<E> all = processables(); 
    return all.size() > 0 ? all.get(0) : null; 
} 

public final E last() { 
    final List<E> all = processables(); 
    final int idx = all.size() - 1; 
    return idx >= 0 ? all.get(idx) : null; 
} 

public final E get(final int index) { 
    final List<E> all = processables(); 
    return all.size() > index ? all.get(index) : null; 
} 

}

此外,我收到一个错误,列出所有= IProcessable;这是列表

+0

您的接口方法需要丢失“public”访问器。 – 2015-01-21 02:37:17

回答

4
class Query<E> : ObjectQuery, Processable<E> 
{ 
    public E 
} 
+0

感谢这项工作 – Fox 2015-01-21 02:25:46

0

你的接口声明需要从它删除公共辅助功能的东西,我改变你的获取方法为一个索引器。我还将您的一些方法更改为属性,并将可处理程序更改为IEnumerable Get属性:

public class Query<E>: ObjectQuery, IProcessable<E> 
{ 
    public E this[int index] 
    { 
     get { throw new NotImplementedException(); } 
    } 

    public E First 
    { 
     get { throw new NotImplementedException(); } 
    } 

    public E Last 
    { 
     get { throw new NotImplementedException(); } 
    } 

    IEnumerable<E> IProcessable<E>.Processables 
    { 
     get { throw new NotImplementedException(); } 
    } 
} 

interface IProcessable<E> 
{ 
    E this[int index] { get; } 
    E First { get; } 
    E Last { get; } 
    IEnumerable<E> Processables { get; } 
} 
+0

这也适用于我认为你写的东西可能更合适。 – Fox 2015-01-21 02:49:16