java - 从 Guava 集合中继承 HashBasedTable

标签 java gwt guava

我有一个多级 map 需求,我正在使用 Guava Table 。更准确地说是 HashBasedTable。

由于我的代码需要对此数据集进行大量自定义处理,因此我想实现一个派生类,例如EventActionRuleTable 持有一个事件映射 - 一个源的 Action 对象。

像这样

HashMap<Source , Map<Event, Action> ruleMap ; 

我将上面的替换为

Table<Source, Event , Action> ruleTable = HashBasedTable.create();

但是为了保留我所有的自定义代码,我想将 HashBasedTable 子类化,但我认为这根本不可能。

因此我选择与代表一起去,即

public EventActionRule extends Table<Source, Event, Action>{

private HashBasedTable<Source, Event, Action> backup = null ;

public HashBasedTable<Source, Event, Action> getBackupTable() {

    if (backupTable == null) {
        backupTable = HashBasedTable.create() ;
    }

    return backupTable;
}  

@Override
public boolean isEmpty() {
    return getBackupTable().isEmpty();
}

/**
All other methods of Table interface overridden to delegate calls to backup instance
*/
  ....
}
  1. 这种方法是否正确?如果不是,你能列出问题吗?任何替代方法?

  2. HashBasedTable Gwt 序列化兼容吗?我在问,因为 HashBasedTable 内部使用的两个备份映射都用 @GwtTransient 注释进行了注释。

最佳答案

广告 1. 您的方法是正确的,尽管您可以使用内置的 Guava 解决方案来使用委托(delegate) - Forwarding Decorators :

For all the various collection interfaces, Guava provides Forwarding abstract classes to simplify using the decorator pattern.

在你的例子中,ForwardingTable正在等你:

public static class EventActionRule extends ForwardingTable<Source, Event, Action> {

    private Table<Source, Event, Action> delegate = HashBasedTable.create();

    @Override
    protected Table<Source, Event, Action> delegate() {
        return delegate;
    }

    // just an example: isEmpty (and other methods) is ready to be overriden
    @Override
    public boolean isEmpty() {
        boolean isEmpty = delegate().isEmpty();
        System.out.println("Was map empty? " + isEmpty);
        return isEmpty;
    }
}

广告。 2. 是的,HashBasedTable 在 GWT 下是可序列化的。

关于java - 从 Guava 集合中继承 HashBasedTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29349281/

相关文章:

java - Jersey(Java 的 REST 框架)在生产中广泛使用吗?

java - 从 java 应用程序 : Relaying denied 发送电子邮件时出现异常

Java多态方法

java - 无法删除比较我的 SQLite 数据库 Android 中的日期的行

gwt - 复制 <g :tab> element of GWT's TabLayoutPanel

java - 适用于 Eclipse Luna 的 Google 插件?

java - 如何使用 GWT 或 Javascript 强调 DIV 内的文本 (innerText) :

java - 如何将 MultiMap<String, String> 转换为 MultiMap<String,Integer>?

Java 8 可选的 asSet()

java - 如何检查至少一个 boolean 值是否为真?