ContentAddition.java

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