-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small Refactoring Changes - First Contribution on Github Open Source #493
base: master
Are you sure you want to change the base?
Changes from all commits
b8dbcb1
9a5a0fc
474cefe
0c36ee7
7212659
cfeb187
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -74,21 +74,10 @@ public void updateFromSettings(PathType type) { | |||||||
|
||||||||
public synchronized void setCustom(Chatty.PathType type, Path path, String info, boolean requireExists) { | ||||||||
ChattyPath p = get(type); | ||||||||
if (!path.isAbsolute()) { | ||||||||
path = Paths.get(System.getProperty("user.dir"), path.toString()); | ||||||||
} | ||||||||
if (requireExists && !Files.isDirectory(path)) { | ||||||||
p.customDir = null; | ||||||||
p.invalidCustomDir = path.toString(); | ||||||||
p.customInfo = info; | ||||||||
} | ||||||||
else { | ||||||||
p.customDir = path; | ||||||||
p.invalidCustomDir = null; | ||||||||
p.customInfo = info; | ||||||||
} | ||||||||
p.setCustomValuesForObject(path,info,requireExists); | ||||||||
} | ||||||||
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be mindful of adding extra blank lines if the codebase otherwise separates functions/methods by a single blank line.
Suggested change
|
||||||||
public void setCustom(Chatty.PathType type, String path, String info, boolean requireExists) { | ||||||||
setCustom(type, Paths.get(path), info, requireExists); | ||||||||
} | ||||||||
|
@@ -169,6 +158,18 @@ public Path get() { | |||||||
return defaultDir; | ||||||||
} | ||||||||
|
||||||||
public synchronized void setCustomValuesForObject(Path path, String info, Boolean requireExists){ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Existing code all has a space between
Suggested change
|
||||||||
if (!path.isAbsolute()) { | ||||||||
path = Paths.get(System.getProperty("user.dir"), path.toString()); | ||||||||
} | ||||||||
this.customDir = path; | ||||||||
this.invalidCustomDir = null; | ||||||||
this.customInfo = info; | ||||||||
if (requireExists && !Files.isDirectory(path)) { | ||||||||
this.customDir = null; | ||||||||
this.invalidCustomDir = path.toString(); | ||||||||
} | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, be mindful of actually adding a blank line to separate methods if that’s what the surrounding/existing code does.
Suggested change
|
||||||||
public Path getAndCreate() { | ||||||||
Path path = get(); | ||||||||
path.toFile().mkdirs(); | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -72,6 +72,57 @@ public interface ParseChannelHelper { | |||||||
* @param prepend Whether to prepend # if necessary | ||||||||
* @return Set of channels sorted as in the String | ||||||||
*/ | ||||||||
|
||||||||
|
||||||||
Comment on lines
+75
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other methods don’t have empty lines between their doc comments and the method itself.
Suggested change
|
||||||||
private static HashMap<String,Boolean> setPossibleChannelType(String[] catSplit, HashMap<String,Boolean> channelType){ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The one other place a Also
Suggested change
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other methods don’t seem to have a blank line at their beginning.
Suggested change
|
||||||||
for (int i = 1; i < catSplit.length; i++) { | ||||||||
if (catSplit[i].equals("#")) { | ||||||||
channelType.put("onlyChans",true); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter spaces. |
||||||||
} | ||||||||
else if (catSplit[i].equals("!#")) { | ||||||||
channelType.put("noChans",true); | ||||||||
|
||||||||
} | ||||||||
else if (catSplit[i].equals("live")) { | ||||||||
channelType.put("onlyLive",true); | ||||||||
} | ||||||||
} | ||||||||
return channelType; | ||||||||
} | ||||||||
|
||||||||
private static List<String> storePossibleChannelNamesToList(String cat, HashMap<String,Boolean> channelType){ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
List<String> chans = new ArrayList<>(); | ||||||||
if (cat.equals("*")) { | ||||||||
chans = new ArrayList<>(parseChannelHelper.getFavorites()); | ||||||||
} | ||||||||
else { | ||||||||
for (String name : parseChannelHelper.getNamesByCategory(cat)) { | ||||||||
if ((!channelType.get("noChans") || !name.startsWith("#")) | ||||||||
&& (!channelType.get("onlyChans") || name.startsWith("#"))) { | ||||||||
chans.add(name); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
return chans; | ||||||||
} | ||||||||
|
||||||||
private static void getPossibleChannelNames(String channel, boolean prepend, Set<String> result){ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
String[] channelTypes = new String[]{"noChans","onlyChans","onlyLive"}; | ||||||||
HashMap<String,Boolean> channelTypeHM = new HashMap<>(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
for(String channelType: channelTypes){ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
channelTypeHM.put(channelType,false); | ||||||||
} | ||||||||
String[] catSplit = channel.substring(1, channel.length() - 1).split(" "); | ||||||||
String cat = catSplit[0]; | ||||||||
channelTypeHM = setPossibleChannelType(catSplit,channelTypeHM); | ||||||||
List<String> chans = storePossibleChannelNamesToList(cat,channelTypeHM); | ||||||||
for (String chan : chans) { | ||||||||
if (!channelTypeHM.get("onlyLive") || parseChannelHelper.isStreamLive(Helper.toStream(chan))) { | ||||||||
addValidChannel(chan, prepend, result); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, whitespace helps readability, esp. if there’s consistency in how they’re used. :)
Suggested change
|
||||||||
public static Set<String> parseChannelsFromString(String channels, boolean prepend) { | ||||||||
String[] parts = channels.split(","); | ||||||||
Set<String> result = new LinkedHashSet<>(); | ||||||||
|
@@ -82,39 +133,7 @@ public static Set<String> parseChannelsFromString(String channels, boolean prepe | |||||||
addValidChannel(channel, prepend, result); | ||||||||
} | ||||||||
else if (channel.startsWith("[") && channel.endsWith("]") && channel.length() > 2 && parseChannelHelper != null) { | ||||||||
String[] catSplit = channel.substring(1, channel.length() - 1).split(" "); | ||||||||
String cat = catSplit[0]; | ||||||||
boolean noChans = false; | ||||||||
boolean onlyChans = false; | ||||||||
boolean onlyLive = false; | ||||||||
for (int i = 1; i < catSplit.length; i++) { | ||||||||
if (catSplit[i].equals("#")) { | ||||||||
onlyChans = true; | ||||||||
} | ||||||||
else if (catSplit[i].equals("!#")) { | ||||||||
noChans = true; | ||||||||
} | ||||||||
else if (catSplit[i].equals("live")) { | ||||||||
onlyLive = true; | ||||||||
} | ||||||||
} | ||||||||
List<String> chans = new ArrayList<>(); | ||||||||
if (cat.equals("*")) { | ||||||||
chans = new ArrayList<>(parseChannelHelper.getFavorites()); | ||||||||
} | ||||||||
else { | ||||||||
for (String name : parseChannelHelper.getNamesByCategory(cat)) { | ||||||||
if ((!noChans || !name.startsWith("#")) | ||||||||
&& (!onlyChans || name.startsWith("#"))) { | ||||||||
chans.add(name); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
for (String chan : chans) { | ||||||||
if (!onlyLive || parseChannelHelper.isStreamLive(Helper.toStream(chan))) { | ||||||||
addValidChannel(chan, prepend, result); | ||||||||
} | ||||||||
} | ||||||||
getPossibleChannelNames(channel,prepend,result); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter spaces.
Suggested change
|
||||||||
} | ||||||||
} | ||||||||
return result; | ||||||||
|
@@ -641,62 +660,8 @@ public static final void main(String[] args) { | |||||||
System.out.println(nf.format(Math.round(74/30.0)*30/60.0)); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* Checks if the id matches the given User. The id can be one of: $mod, | ||||||||
* $sub, $turbo, $admin, $broadcaster, $staff, $bot. If the user has the | ||||||||
* appropriate user status, this returns true. If the id is unknown or the | ||||||||
* user doesn't have the required status, this returns false. | ||||||||
* | ||||||||
* @param id The id that is required | ||||||||
* @param user The User object to check against | ||||||||
* @return true if the id is known and matches the User, false otherwise | ||||||||
*/ | ||||||||
public static boolean matchUserStatus(String id, User user) { | ||||||||
if (id.equals("$mod")) { | ||||||||
if (user.isModerator()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$sub")) { | ||||||||
if (user.isSubscriber()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$turbo")) { | ||||||||
if (user.hasTurbo()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$admin")) { | ||||||||
if (user.isAdmin()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$broadcaster")) { | ||||||||
if (user.isBroadcaster()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$staff")) { | ||||||||
if (user.isStaff()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$bot")) { | ||||||||
if (user.isBot()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$globalmod")) { | ||||||||
if (user.isGlobalMod()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$anymod")) { | ||||||||
if (user.isAdmin() || user.isBroadcaster() || user.isGlobalMod() | ||||||||
|| user.isModerator() || user.isStaff()) { | ||||||||
return true; | ||||||||
} | ||||||||
} else if (id.equals("$vip")) { | ||||||||
if (user.hasTwitchBadge("vip")) { | ||||||||
return true; | ||||||||
} | ||||||||
} | ||||||||
return false; | ||||||||
} | ||||||||
|
||||||||
|
||||||||
|
||||||||
public static String checkHttpUrl(String url) { | ||||||||
if (url == null) { | ||||||||
return null; | ||||||||
|
@@ -1210,4 +1175,131 @@ public static boolean isBeforeChatCommandsShutoff() { | |||||||
return Instant.now().isBefore(CHAT_COMMAND_SHUTOFF); | ||||||||
} | ||||||||
|
||||||||
public interface UserStatusMatcher { | ||||||||
boolean matchUserStatus(User user); | ||||||||
} | ||||||||
|
||||||||
public static class ModeratorMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isModerator(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class SubscriberMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isSubscriber(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class TurboMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.hasTurbo(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class AdminMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isAdmin(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class BroadcasterMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isBroadcaster(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class StaffMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isStaff(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class BotMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isBot(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class GlobalModMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isGlobalMod(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class AnyModMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.isAdmin() || user.isBroadcaster() || user.isGlobalMod() | ||||||||
|| user.isModerator() || user.isStaff(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public static class VipMatcher implements UserStatusMatcher { | ||||||||
@Override | ||||||||
public boolean matchUserStatus(User user) { | ||||||||
return user.hasTwitchBadge("vip"); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* Checks if the id matches the given User. The id can be one of: $mod, | ||||||||
* $sub, $turbo, $admin, $broadcaster, $staff, $bot. If the user has the | ||||||||
* appropriate user status, this returns true. If the id is unknown or the | ||||||||
* user doesn't have the required status, this returns false. | ||||||||
* | ||||||||
* @param id The id that is required | ||||||||
* @param user The User object to check against | ||||||||
* @return true if the id is known and matches the User, false otherwise | ||||||||
*/ | ||||||||
|
||||||||
public static boolean matchUserStatus(String id, User user) { | ||||||||
|
||||||||
|
||||||||
UserStatusMatcher matcher = null; | ||||||||
|
||||||||
switch (id) { | ||||||||
case "$mod": | ||||||||
matcher = new ModeratorMatcher(); | ||||||||
break; | ||||||||
case "$sub": | ||||||||
matcher = new SubscriberMatcher(); | ||||||||
break; | ||||||||
case "$turbo": | ||||||||
matcher = new TurboMatcher(); | ||||||||
break; | ||||||||
case "$admin": | ||||||||
matcher = new AdminMatcher(); | ||||||||
break; | ||||||||
case "$broadcaster": | ||||||||
matcher = new BroadcasterMatcher(); | ||||||||
break; | ||||||||
case "$staff": | ||||||||
matcher = new StaffMatcher(); | ||||||||
break; | ||||||||
case "$bot": | ||||||||
matcher = new BotMatcher(); | ||||||||
break; | ||||||||
case "$globalmod": | ||||||||
matcher = new GlobalModMatcher(); | ||||||||
break; | ||||||||
case "$anymod": | ||||||||
matcher = new AnyModMatcher(); | ||||||||
break; | ||||||||
case "$vip": | ||||||||
matcher = new VipMatcher(); | ||||||||
break; | ||||||||
} | ||||||||
|
||||||||
return matcher != null ? matcher.matchUserStatus(user): false; | ||||||||
} | ||||||||
|
||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Existing code uses a space after comma for passing parameters.