attribute1과 attribute2의 조합을 나타 내기 위해 사용자 지정 쌍 클래스가있는 Multimaps.index()를 사용합니다. 당신과 같은 인터페이스가 있다면 코드가/더 많은 청소기 될 것이라고
/**
* 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();
}
는, 그 속성 1을 가정하고 속성 2는 널 (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);
}
}
각 속성 조합마다 정확히 하나의 항목이 목록에 있습니까? 또한 세 개의 "Object"클래스가 수퍼 클래스를 공유합니까? –
이들은 수퍼 클래스를 공유 할 수도 있고하지 않을 수도 있으며 각 목록에 존재하지 않을 수도 있습니다. – JustinM