番石榴集团按键

时间:2012-02-27 02:50:35

标签: guava

仍在学习Guava API的所有功能。想要在那里拍摄,看看是否有任何关于如何做到这一点的建议。目前的图片如下所示:List < ObjectA >, List< ObjectB >, List< ObjectC >

ObjectA {
    Integer attribute1;
    String attribute2;
    ….
}  
ObjectB {
    Integer attribute1;
    String attribute2;
    ….
}  
ObjectC {
    Integer attribute1;
    String attribute2;
    ….
}

我想通过attribute1和attribute2获取3个列表并将对象组合在一起,从而创建如下内容:

123|”Whatever” : { Object A, ObjectB, ObjectC}  
456|”Something” : { Object A, ObjectB, ObjectC}

显而易见的* Multimap是回归,但构建它的过程可能有多个解决方案。关于如何处理这个的想法?

1 个答案:

答案 0 :(得分:2)

我会将Multimaps.index()与自定义对类一起使用来表示attribute1和attribute2的组合。

/**
 * A "pair" class that contains an {@code attribute1} and an {@code attribute2}. Mainly used as a {@link Map} key.
 */
@Immutable
public class Attribute1AndAttribute2 {

    @Nullable
    private final Integer attribute1;
    @Nullable
    private final String attribute2;

    public Attribute1AndAttribute2(@Nullable Integer attribute1,
                                   @Nullable String attribute2) {
        this.attribute1 = attribute1;
        this.attribute2 = attribute2;
    }

    @Nullable
    public Integer getAttribute1() {
        return attribute1;
    }

    @Nullable
    public String getAttribute2() {
        return attribute2;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Attribute1AndAttribute2) {
            Attribute1AndAttribute2 that = (Attribute1AndAttribute2) obj;
            return Objects.equal(this.attribute1, that.attribute1)
                    && Objects.equal(this.attribute2, that.attribute2);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(attribute1, attribute2);
    }

}


/**
 * Static utility methods pertaining to {@link ObjectA}, {@link ObjectB}, and {@link ObjectC}'s {@code attribute1} and
 * {@code attribute2}.
 */
public final class Attribute1AndAttribute2Utils {
    private Attribute1AndAttribute2Utils() { /* prevents instantiation */ }

    public static Multimap<Attribute1AndAttribute2, Object> groupedByAttribute1AndAttribute2(List<ObjectA> as, List<ObjectB> bs, List<ObjectC> cs) {
        Iterable<Object> abcs = Iterables.concat(as, bs, cs);
        return Multimaps.index(abcs, Attribute1AndAttribute2ForObjectFunction.INSTANCE);
    }

    // enum singleton pattern
    private enum Attribute1AndAttribute2ForObjectFunction implements Function<Object, Attribute1AndAttribute2> {
        INSTANCE;

        @Override
        public Attribute1AndAttribute2 apply(@Nullable Object object) {
            if (object instanceof ObjectA) {
                ObjectA objectA = (ObjectA) object;
                return new Attribute1AndAttribute2(objectA.attribute1, objectA.attribute2);
            } else if (object instanceof ObjectB) {
                ObjectB objectB = (ObjectB) object;
                return new Attribute1AndAttribute2(objectB.attribute1, objectB.attribute2);
            } else if (object instanceof ObjectC) {
                ObjectC objectC = (ObjectC) object;
                return new Attribute1AndAttribute2(objectC.attribute1, objectC.attribute2);
            } else {
                throw new RuntimeException("Object must be ObjectA, ObjectB, or ObjectC, but was " + object);
            }
        }
    }

}

请注意,如果您使用的界面如下,则代码会更清晰/更好:

public interface HasAttribute1AndAttribute2 {
    Integer getAttribute1();
    String getAttribute2();
}

另外,我假设attribute1和attribute2可能为null。如果它们永远不为null,请考虑将对类更改为:

/**
 * A "pair" class that contains an {@code attribute1} and an {@code attribute2}. Mainly used as a {@link Map} key.
 */
@Immutable
public class Attribute1AndAttribute2 {

    @Nonnull
    private final Integer attribute1;
    @Nonnull
    private final String attribute2;

    public Attribute1AndAttribute2(@Nonnull Integer attribute1, @Nonnull String attribute2) {
        this.attribute1 = checkNotNull(attribute1);
        this.attribute2 = checkNotNull(attribute2);
    }

    @Nonnull
    public Integer getAttribute1() {
        return attribute1;
    }

    @Nonnull
    public String getAttribute2() {
        return attribute2;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Attribute1AndAttribute2) {
            Attribute1AndAttribute2 that = (Attribute1AndAttribute2) obj;
            return this.attribute1.equals(that.attribute1)
                    && this.attribute2.equals(that.attribute2);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(attribute1, attribute2);
    }
}