Java 泛型的正确使用
从我的脸上Java 泛型的正确使用,java,generics,primefaces,Java,Generics,Primefaces,从我的脸上 public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable { 无论如何,Luiggi Mendoza和Paul Bellora给了我一个关于泛型的重要概念,我认为类的正确定义是 public class LazyRetiDataModel extends LazyDataModel<R
public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable {
无论如何,Luiggi Mendoza和Paul Bellora给了我一个关于泛型的重要概念,我认为类的正确定义是
public class LazyRetiDataModel extends LazyDataModel<Rete> {
公共类LazyritDataModel扩展了LazyDataModel{
LazyriteDataModel不使用泛型。您将收到一条警告,因为您正在强制转换为原始类型
LazyriteDataModel
。我建议您改为:
if (lazyModel != null) {
((LazyRetiDataModel<Rete>)lazyModel).setFilters(mappa);
}
if(lazyModel!=null){
(lazyritidatamodel)lazyModel.setFilters(mappa);
}
通常,这会导致未经检查的强制转换警告,但这实际上是该规则的一个例外,因为编译器已经知道泛型类型是Rete
请注意,我也同意Luiggi Mendoza关于
LazyriteDataModel扩展LazyDataModel
可能是一个打字错误的评论-您想要LazyriteDataModel扩展LazyDataModel
为什么公共类LazyriteDataModel
首先是泛型的?它定义了超类的泛型参数,如果t是它唯一的方法(setFilters),然后只需删除。
更新:
如果没有使用新定义的通用方法,那么它应该被删除。正如你提到的,它覆盖了一些无法使用新方法的方法(如果我是正确的),因此我仍然认为你可能不需要新方法。汤姆·霍丁(Tom Hawtin)提出的建议,马克·彼得斯(Mark Peters)也用他的话改变了我的看法。我定义了
private LazyRetiDataModel lazyModel;
私人Lazyritidatamodel lazyModel;
无论如何,Luiggi Mendoza和Paul Bellora给了我一个关于泛型的重要概念,我认为类的正确定义是
公共类LazyritDataModel扩展了LazyDataModel{
LazyriteDataModel不使用泛型
java泛型primefaces最好不要强制转换。只需更改
lazyModel
的类型。在我看来,它应该是LazyriteDataModel扩展LazyDataModel
或LazyriteDataModel扩展LazyDataModel
@Luigimendoza好的,我尝试一下,您的建议在原始示例中是正确的,我发现了这种扩展。i th主要动机是使用“@Override”公共列表加载(int-first、int-pageSize、String-sortField、SortOrder-SortOrder、Map-filters){同意Tom的说法。如果你要盲目地假设lazyModel
是一个LazyritDataModel
,那么就声明它是这样并获得静态类型安全性。@MarkPeters,@TomHawtin-tackline使用primefaces,我需要将lezy模型返回为公共LazyDataModel getLazyModel(){返回lazyModel;}
在PrimeFace中有一个对getLazyModel的调用,它等待一个LazyDataModel并显示一个对象表T,其中T是ReteAs,我在comment@luigimendoza中告诉过,在LazyDataModel中使用是强制性的。该类使用通用的everywhere示例公共列表加载(
在其当前形式中,这看起来像一个注释。请更新它,使其看起来像一个真正的建设性答案。不,它必须验证惰性数据模型的方法。现在,我阅读了完整的超类。您真的不必将LazyriteDataModel设置为泛型。
/*
* Copyright 2009-2012 Prime Teknoloji.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.primefaces.model;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.faces.model.DataModel;
/**
* Custom lazy loading DataModel to deal with huge datasets
*/
public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable {
private int rowIndex = -1;
private int rowCount;
private int pageSize;
private List<T> data;
public LazyDataModel() {
super();
}
public boolean isRowAvailable() {
if(data == null) {
return false;
}
return rowIndex >= 0 && rowIndex < data.size();
}
public int getRowCount() {
return rowCount;
}
public T getRowData() {
return data.get(rowIndex);
}
public int getRowIndex() {
return this.rowIndex;
}
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
}
public Object getWrappedData() {
return data;
}
public void setWrappedData(Object list) {
this.data = (List) list;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
throw new UnsupportedOperationException("Lazy loading is not implemented.");
}
public List<T> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String,String> filters) {
throw new UnsupportedOperationException("Lazy loading is not implemented.");
}
public T getRowData(String rowKey) {
throw new UnsupportedOperationException("getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used.");
}
public Object getRowKey(T object) {
throw new UnsupportedOperationException("getRowKey(T object) must be implemented when basic rowKey algorithm is not used.");
}
}
warning: [unchecked] unchecked call to setFilters(Map<String,String>) as a member of the raw type LazyRetiDataModel
((LazyRetiDataModel) lazyModel).setFilters( mappa );
private LazyRetiDataModel lazyModel;
public class LazyRetiDataModel extends LazyDataModel<Rete> {
if (lazyModel != null) {
((LazyRetiDataModel<Rete>)lazyModel).setFilters(mappa);
}