Skip to content

Commit 639c773

Browse files
committed
Add ISUPPORT MAXLIST
1 parent 66fc7af commit 639c773

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* * Copyright (C) 2013-2019 Matt Baxter https://kitteh.org
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use, copy,
8+
* modify, merge, publish, distribute, sublicense, and/or sell copies
9+
* of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.kitteh.irc.client.library.defaults.element.isupport;
25+
26+
import org.checkerframework.checker.nullness.qual.NonNull;
27+
import org.checkerframework.checker.nullness.qual.Nullable;
28+
import org.kitteh.irc.client.library.Client;
29+
import org.kitteh.irc.client.library.element.ISupportParameter;
30+
31+
import java.util.Collections;
32+
import java.util.HashSet;
33+
import java.util.Optional;
34+
import java.util.Set;
35+
36+
/**
37+
* Default implementation of {@link CaseMapping}.
38+
*/
39+
public class DefaultISupportMaxList extends DefaultISupportParameterValueRequired implements ISupportParameter.MaxList {
40+
/**
41+
* Default implementation of {@link MaxList.LimitData}.
42+
*/
43+
public class DefaultLimitData implements MaxList.LimitData {
44+
private final int limit;
45+
private final Set<Character> modes;
46+
47+
/**
48+
* Constructs this limit data.
49+
*
50+
* @param modes modes
51+
* @param limit limit
52+
*/
53+
public DefaultLimitData(@NonNull Set<Character> modes, int limit) {
54+
this.limit = limit;
55+
this.modes = Collections.unmodifiableSet(modes);
56+
}
57+
58+
@Override
59+
public int getLimit() {
60+
return this.limit;
61+
}
62+
63+
@Override
64+
public @NonNull Set<Character> getModes() {
65+
return this.modes;
66+
}
67+
}
68+
69+
private final Set<MaxList.LimitData> data;
70+
71+
/**
72+
* Constructs the object.
73+
*
74+
* @param client client
75+
* @param name parameter name
76+
* @param value parameter value, if present
77+
*/
78+
public DefaultISupportMaxList(@NonNull Client client, @NonNull String name, @Nullable String value) {
79+
super(client, name, value);
80+
Set<MaxList.LimitData> limitData = new HashSet<>();
81+
for (String limit : value.split(",")) {
82+
String[] split = limit.split(":");
83+
Set<Character> modes = new HashSet<>();
84+
for (char c : split[0].toCharArray()) {
85+
modes.add(c);
86+
}
87+
limitData.add(new DefaultLimitData(modes, Integer.parseInt(split[1])));
88+
}
89+
this.data = Collections.unmodifiableSet(limitData);
90+
}
91+
92+
@Override
93+
public @NonNull Set<LimitData> getAllLimitData() {
94+
return this.data;
95+
}
96+
97+
@Override
98+
public int getLimit(char c) {
99+
return this.data.stream().filter(d -> d.getModes().contains(c)).mapToInt(LimitData::getLimit).findFirst().orElse(-1);
100+
}
101+
102+
@Override
103+
public @NonNull Optional<LimitData> getLimitData(char c) {
104+
return this.data.stream().filter(d -> d.getModes().contains(c)).findFirst();
105+
}
106+
}

src/main/java/org/kitteh/irc/client/library/defaults/feature/DefaultISupportManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportHostLen;
4040
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportInvEx;
4141
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportKickLen;
42+
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportMaxList;
4243
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportMaxTargets;
4344
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportModes;
4445
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportNetwork;
@@ -80,6 +81,7 @@ public DefaultISupportManager(Client.WithManagement client) {
8081
this.registerParameter(ISupportParameter.HostLen.NAME, DefaultISupportHostLen::new);
8182
this.registerParameter(ISupportParameter.InvEx.NAME, DefaultISupportInvEx::new);
8283
this.registerParameter(ISupportParameter.KickLen.NAME, DefaultISupportKickLen::new);
84+
this.registerParameter(ISupportParameter.MaxList.NAME, DefaultISupportMaxList::new);
8385
this.registerParameter(ISupportParameter.MaxTargets.NAME, DefaultISupportMaxTargets::new);
8486
this.registerParameter(ISupportParameter.Modes.NAME, DefaultISupportModes::new);
8587
this.registerParameter(ISupportParameter.Network.NAME, DefaultISupportNetwork::new);

src/main/java/org/kitteh/irc/client/library/element/ISupportParameter.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,58 @@ interface InvEx extends ISupportParameter {
242242
String NAME = "INVEX";
243243
}
244244

245+
/**
246+
* Represents maximum list sizes for type A modes.
247+
*/
248+
interface MaxList extends ISupportParameter {
249+
/**
250+
* Data on a particular limit.
251+
*/
252+
interface LimitData {
253+
/**
254+
* Gets the limit for this data.
255+
*
256+
* @return limit
257+
*/
258+
int getLimit();
259+
260+
/**
261+
* Gets the modes for this data.
262+
*
263+
* @return modes
264+
*/
265+
@NonNull Set<Character> getModes();
266+
}
267+
268+
/**
269+
* Parameter name.
270+
*/
271+
String NAME = "MAXLIST";
272+
273+
/**
274+
* Gets all the limit data specified.
275+
*
276+
* @return collection of limit data
277+
*/
278+
@NonNull Set<LimitData> getAllLimitData();
279+
280+
/**
281+
* Gets the maximum limit for a given mode.
282+
*
283+
* @param c mode character
284+
* @return limit or -1 if not specified
285+
*/
286+
int getLimit(char c);
287+
288+
/**
289+
* Gets the limit data for a given mode.
290+
*
291+
* @param c mode character
292+
* @return limit data if specified
293+
*/
294+
@NonNull Optional<LimitData> getLimitData(char c);
295+
}
296+
245297
/**
246298
* Represents limits to type A mode lists.
247299
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.kitteh.irc.client.library.defaults.feature.isupport;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.kitteh.irc.client.library.Client;
6+
import org.kitteh.irc.client.library.defaults.element.isupport.DefaultISupportMaxList;
7+
import org.kitteh.irc.client.library.element.ISupportParameter;
8+
import org.mockito.Mockito;
9+
10+
import java.util.Optional;
11+
12+
public class MaxListTest {
13+
@Test
14+
public void testMaxList() {
15+
ISupportParameter.MaxList maxList = new DefaultISupportMaxList(Mockito.mock(Client.class), "MAXLIST", "b:60,e:60,I:60");
16+
Assert.assertEquals("b should be 60", 60, maxList.getLimit('b'));
17+
Assert.assertEquals("q should be -1", -1, maxList.getLimit('q'));
18+
Assert.assertEquals("e should be 60", 60, maxList.getLimit('e'));
19+
Assert.assertEquals("I should be 60", 60, maxList.getLimit('I'));
20+
Assert.assertEquals("Should be 3 limit data", 3, maxList.getAllLimitData().size());
21+
Optional<ISupportParameter.MaxList.LimitData> bDataOpt = maxList.getLimitData('b');
22+
Assert.assertTrue("b data should be present", bDataOpt.isPresent());
23+
Assert.assertFalse("q data should not be present", maxList.getLimitData('q').isPresent());
24+
ISupportParameter.MaxList.LimitData bData = bDataOpt.get();
25+
Assert.assertEquals("b via data should be 60", 60, bData.getLimit());
26+
Assert.assertEquals("Should be one char in b data", 1, bData.getModes().size());
27+
Assert.assertTrue("b should be in b data", bData.getModes().contains('b'));
28+
}
29+
30+
@Test
31+
public void testMaxListMerged() {
32+
ISupportParameter.MaxList maxList = new DefaultISupportMaxList(Mockito.mock(Client.class), "MAXLIST", "bqeI:100");
33+
Assert.assertEquals("b should be 100", 100, maxList.getLimit('b'));
34+
Assert.assertEquals("q should be 100", 100, maxList.getLimit('q'));
35+
Assert.assertEquals("e should be 100", 100, maxList.getLimit('e'));
36+
Assert.assertEquals("I should be 100", 100, maxList.getLimit('I'));
37+
Assert.assertEquals("Should be 1 limit data", 1, maxList.getAllLimitData().size());
38+
Optional<ISupportParameter.MaxList.LimitData> bDataOpt = maxList.getLimitData('b');
39+
Assert.assertTrue("bData should be present", bDataOpt.isPresent());
40+
ISupportParameter.MaxList.LimitData bData = bDataOpt.get();
41+
Assert.assertEquals("b via data should be 60", 100, bData.getLimit());
42+
Assert.assertEquals("Should be 4 chars in b data", 4, bData.getModes().size());
43+
Assert.assertTrue("b should be in b data", bData.getModes().contains('b'));
44+
}
45+
}

0 commit comments

Comments
 (0)