ContentAddition.java

 1package eu.siacs.conversations.xmpp.jingle;
 2
 3import com.google.common.base.MoreObjects;
 4import com.google.common.base.Objects;
 5import com.google.common.collect.Collections2;
 6import com.google.common.collect.ImmutableSet;
 7
 8import java.util.Set;
 9
10import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
11
12public final class ContentAddition {
13
14    public final Direction direction;
15    public final Set<Summary> summary;
16
17    private ContentAddition(Direction direction, Set<Summary> summary) {
18        this.direction = direction;
19        this.summary = summary;
20    }
21
22    public Set<Media> media() {
23        return ImmutableSet.copyOf(Collections2.transform(summary, s -> s.media));
24    }
25
26    public static ContentAddition of(final Direction direction, final RtpContentMap rtpContentMap) {
27        return new ContentAddition(direction, summary(rtpContentMap));
28    }
29
30    public static Set<Summary> summary(final RtpContentMap rtpContentMap) {
31        return ImmutableSet.copyOf(
32                Collections2.transform(
33                        rtpContentMap.contents.entrySet(),
34                        e -> {
35                            final RtpContentMap.DescriptionTransport dt = e.getValue();
36                            return new Summary(e.getKey(), dt.description.getMedia(), dt.senders);
37                        }));
38    }
39
40    @Override
41    public String toString() {
42        return MoreObjects.toStringHelper(this)
43                .add("direction", direction)
44                .add("summary", summary)
45                .toString();
46    }
47
48    public enum Direction {
49        OUTGOING,
50        INCOMING
51    }
52
53    public static final class Summary {
54        public final String name;
55        public final Media media;
56        public final Content.Senders senders;
57
58        private Summary(final String name, final Media media, final Content.Senders senders) {
59            this.name = name;
60            this.media = media;
61            this.senders = senders;
62        }
63
64        @Override
65        public boolean equals(Object o) {
66            if (this == o) return true;
67            if (o == null || getClass() != o.getClass()) return false;
68            Summary summary = (Summary) o;
69            return Objects.equal(name, summary.name)
70                    && media == summary.media
71                    && senders == summary.senders;
72        }
73
74        @Override
75        public int hashCode() {
76            return Objects.hashCode(name, media, senders);
77        }
78
79        @Override
80        public String toString() {
81            return MoreObjects.toStringHelper(this)
82                    .add("name", name)
83                    .add("media", media)
84                    .add("senders", senders)
85                    .toString();
86        }
87    }
88}