diff options
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/ChannelNode.java | 153 | ||||
| -rw-r--r-- | src/main/java/IChannelNode.java | 14 |
2 files changed, 163 insertions, 4 deletions
diff --git a/src/main/java/ChannelNode.java b/src/main/java/ChannelNode.java new file mode 100644 index 0000000..e984cf0 --- /dev/null +++ b/src/main/java/ChannelNode.java | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | import java.util.ArrayList; | ||
| 2 | import java.util.List; | ||
| 3 | import java.util.Map; | ||
| 4 | import java.util.TreeMap; | ||
| 5 | |||
| 6 | public class ChannelNode implements IChannelNode { | ||
| 7 | private Map<IChannelNode, Integer> incoming; | ||
| 8 | private Map<IChannelNode, Integer> outgoing; | ||
| 9 | |||
| 10 | public ChannelNode() { | ||
| 11 | incoming = new TreeMap<>(); | ||
| 12 | outgoing = new TreeMap<>(); | ||
| 13 | } | ||
| 14 | |||
| 15 | @Override | ||
| 16 | public void setConnections(Map<IChannelNode, Integer> conmap, Direction dir) throws IllegalArgumentException { | ||
| 17 | if(conmap == null) throw new IllegalArgumentException("connection map is null"); | ||
| 18 | if(dir == null) throw new IllegalArgumentException("direction is null"); | ||
| 19 | if(conmap.containsKey(this)) throw new IllegalArgumentException("connection map contains this node"); | ||
| 20 | |||
| 21 | int dv = dir.getVal(); | ||
| 22 | if(dv >= Direction.INCOMING.getVal()) incoming = conmap; | ||
| 23 | if(dv >= Direction.OUTGOING.getVal()) outgoing = conmap; | ||
| 24 | } | ||
| 25 | |||
| 26 | @Override | ||
| 27 | public void setNumConnections(IChannelNode node, Direction dir, int num) throws IllegalArgumentException { | ||
| 28 | if(node == null) throw new IllegalArgumentException("node is null"); | ||
| 29 | if(dir == null) throw new IllegalArgumentException("dir is null"); | ||
| 30 | if(num < 0) throw new IllegalArgumentException("num < 0"); | ||
| 31 | |||
| 32 | int dv = dir.getVal(); | ||
| 33 | if(dv >= Direction.INCOMING.getVal()) incoming.put(node, num); | ||
| 34 | if(dv >= Direction.OUTGOING.getVal()) outgoing.put(node, num); | ||
| 35 | } | ||
| 36 | |||
| 37 | @Override | ||
| 38 | public void addConnection(IChannelNode node, Direction dir) { | ||
| 39 | if(node == null) throw new IllegalArgumentException("node is null"); | ||
| 40 | if(node == this) throw new IllegalArgumentException("node is self"); | ||
| 41 | if(dir == null) throw new IllegalStateException("dir is null"); | ||
| 42 | |||
| 43 | int dv = dir.getVal(); | ||
| 44 | if(dv >= Direction.INCOMING.getVal()) incoming.put(node, incoming.get(node) + 1); | ||
| 45 | if(dv >= Direction.OUTGOING.getVal()) outgoing.put(node, outgoing.get(node) + 1); | ||
| 46 | } | ||
| 47 | |||
| 48 | @Override | ||
| 49 | public void addConnections(Iterable<IChannelNode> nodes, Direction dir) { | ||
| 50 | if(nodes == null) throw new IllegalArgumentException("node is null"); | ||
| 51 | if(dir == null) throw new IllegalArgumentException("dir is null"); | ||
| 52 | nodes.forEach((node) -> {if(node == this) throw new IllegalArgumentException("one of the included nodes is this node");}); | ||
| 53 | |||
| 54 | int dv = dir.getVal(), | ||
| 55 | incomingV = Direction.INCOMING.getVal(), | ||
| 56 | outgoingV = Direction.OUTGOING.getVal(); | ||
| 57 | |||
| 58 | nodes.forEach((node) -> { | ||
| 59 | if(dv >= incomingV) incoming.put(node, incoming.get(node) + 1); | ||
| 60 | if(dv >= outgoingV) outgoing.put(node, outgoing.get(node) + 1); | ||
| 61 | }); | ||
| 62 | } | ||
| 63 | |||
| 64 | @Override | ||
| 65 | public void removeConnection(IChannelNode node, Direction dir) { | ||
| 66 | if(node == null) throw new IllegalArgumentException("node is null"); | ||
| 67 | if(node == this) throw new IllegalArgumentException("node is self"); | ||
| 68 | if(dir == null) throw new IllegalStateException("dir is null"); | ||
| 69 | |||
| 70 | int dv = dir.getVal(); | ||
| 71 | if(dv >= Direction.INCOMING.getVal()) incoming.put(node, Math.max(incoming.get(node) - 1, 0)); | ||
| 72 | if(dv >= Direction.OUTGOING.getVal()) outgoing.put(node, Math.max(outgoing.get(node) - 1, 0)); | ||
| 73 | } | ||
| 74 | |||
| 75 | @Override | ||
| 76 | public void removeConnections(Iterable<IChannelNode> nodes, Direction dir) { | ||
| 77 | if(nodes == null) throw new IllegalArgumentException("node is null"); | ||
| 78 | if(dir == null) throw new IllegalArgumentException("dir is null"); | ||
| 79 | nodes.forEach((node) -> {if(node == this) throw new IllegalArgumentException("one of the included nodes is this node");}); | ||
| 80 | |||
| 81 | int dv = dir.getVal(), | ||
| 82 | incomingV = Direction.INCOMING.getVal(), | ||
| 83 | outgoingV = Direction.OUTGOING.getVal(); | ||
| 84 | |||
| 85 | nodes.forEach((node) -> { | ||
| 86 | if(dv >= incomingV) incoming.put(node, Math.max(incoming.get(node) - 1, 0)); | ||
| 87 | if(dv >= outgoingV) outgoing.put(node, Math.max(outgoing.get(node) - 1, 0)); | ||
| 88 | }); | ||
| 89 | } | ||
| 90 | |||
| 91 | @Override | ||
| 92 | public void clearConnections(Direction dir) { | ||
| 93 | if(dir == null) throw new IllegalArgumentException("dir is null"); | ||
| 94 | |||
| 95 | int dv = dir.getVal(); | ||
| 96 | if(dv >= Direction.INCOMING.getVal()) incoming.clear(); | ||
| 97 | if(dv >= Direction.OUTGOING.getVal()) outgoing.clear(); | ||
| 98 | } | ||
| 99 | |||
| 100 | @Override | ||
| 101 | public boolean connectionExists(IChannelNode node, Direction dir) { | ||
| 102 | if(node == null) throw new IllegalArgumentException("node is null"); | ||
| 103 | if(dir == null) throw new IllegalArgumentException("dir is null"); | ||
| 104 | |||
| 105 | switch (dir) { | ||
| 106 | case INCOMING -> {return incoming.containsKey(node);} | ||
| 107 | case OUTGOING -> {return outgoing.containsKey(node);} | ||
| 108 | case BOTH -> {return (incoming.containsKey(node) && outgoing.containsKey(node));} | ||
| 109 | default -> throw new IllegalStateException("got unknown direction"); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | @Override | ||
| 114 | public int getNumConnections(Direction dir) { | ||
| 115 | int total = 0; | ||
| 116 | int dv = dir.getVal(); | ||
| 117 | |||
| 118 | if(dv >= Direction.INCOMING.getVal()) for(int i: incoming.values()) total += i; | ||
| 119 | if(dv >= Direction.OUTGOING.getVal()) for(int i: outgoing.values()) total += i; | ||
| 120 | return total; | ||
| 121 | } | ||
| 122 | |||
| 123 | @Override | ||
| 124 | public Map<IChannelNode, Integer> getIncomingConnections() { | ||
| 125 | return incoming; | ||
| 126 | } | ||
| 127 | |||
| 128 | @Override | ||
| 129 | public Map<IChannelNode, Integer> getOutgoingConnections() { | ||
| 130 | return outgoing; | ||
| 131 | } | ||
| 132 | |||
| 133 | @Override | ||
| 134 | public List<Map<IChannelNode, Integer>> getConnections(Direction dir) { | ||
| 135 | if(dir == null) throw new IllegalArgumentException("dir is null"); | ||
| 136 | |||
| 137 | int dv = dir.getVal(); | ||
| 138 | ArrayList<Map<IChannelNode, Integer>> res = new ArrayList<>(); | ||
| 139 | if(dv >= Direction.INCOMING.getVal()) res.add(incoming); | ||
| 140 | if(dv >= Direction.OUTGOING.getVal()) res.add(outgoing); | ||
| 141 | |||
| 142 | return res; | ||
| 143 | } | ||
| 144 | |||
| 145 | @Override | ||
| 146 | public int compareTo(IChannelNode iChannelNode) { | ||
| 147 | if(iChannelNode == null) throw new IllegalArgumentException("comparison node is null"); | ||
| 148 | int selfTotal = getNumConnections(Direction.BOTH), | ||
| 149 | theirTotal = iChannelNode.getNumConnections(Direction.BOTH); | ||
| 150 | |||
| 151 | return selfTotal - theirTotal; | ||
| 152 | } | ||
| 153 | } | ||
diff --git a/src/main/java/IChannelNode.java b/src/main/java/IChannelNode.java index 463f3ac..baf7572 100644 --- a/src/main/java/IChannelNode.java +++ b/src/main/java/IChannelNode.java | |||
| @@ -1,14 +1,19 @@ | |||
| 1 | import java.util.List; | 1 | import java.util.List; |
| 2 | import java.util.Map; | 2 | import java.util.Map; |
| 3 | 3 | ||
| 4 | public interface IChannelNode { | 4 | public interface IChannelNode extends Comparable<IChannelNode> { |
| 5 | enum Direction { | 5 | enum Direction { |
| 6 | INCOMING, | 6 | INCOMING(0), // Users incoming from outside channel (aka: other channel forwarded this channel's post) |
| 7 | OUTGOING, | 7 | OUTGOING(1), // Users outgoing from this channel (aka: this channel forwarded someone else's post) |
| 8 | BOTH | 8 | BOTH(2); // Modify both incoming and outgoing counts at once |
| 9 | |||
| 10 | private final int val; | ||
| 11 | Direction(int val) {this.val = val;} | ||
| 12 | public int getVal() {return this.val;} | ||
| 9 | } | 13 | } |
| 10 | 14 | ||
| 11 | void setConnections(Map<IChannelNode, Integer> conmap, Direction dir); | 15 | void setConnections(Map<IChannelNode, Integer> conmap, Direction dir); |
| 16 | void setNumConnections(IChannelNode node, Direction dir, int num); | ||
| 12 | void addConnection(IChannelNode node, Direction dir); | 17 | void addConnection(IChannelNode node, Direction dir); |
| 13 | void addConnections(Iterable<IChannelNode> nodes, Direction dir); | 18 | void addConnections(Iterable<IChannelNode> nodes, Direction dir); |
| 14 | void removeConnection(IChannelNode node, Direction dir); | 19 | void removeConnection(IChannelNode node, Direction dir); |
| @@ -16,6 +21,7 @@ public interface IChannelNode { | |||
| 16 | void clearConnections(Direction dir); | 21 | void clearConnections(Direction dir); |
| 17 | 22 | ||
| 18 | boolean connectionExists(IChannelNode node, Direction dir); | 23 | boolean connectionExists(IChannelNode node, Direction dir); |
| 24 | int getNumConnections(Direction dir); | ||
| 19 | Map<IChannelNode, Integer> getIncomingConnections(); | 25 | Map<IChannelNode, Integer> getIncomingConnections(); |
| 20 | Map<IChannelNode, Integer> getOutgoingConnections(); | 26 | Map<IChannelNode, Integer> getOutgoingConnections(); |
| 21 | List<Map<IChannelNode, Integer>> getConnections(Direction dir); | 27 | List<Map<IChannelNode, Integer>> getConnections(Direction dir); |
