Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
27 changes: 14 additions & 13 deletions src/chatty/CustomPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

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.

Suggested change
p.setCustomValuesForObject(path,info,requireExists);
p.setCustomValuesForObject(path, info, requireExists);

}


Copy link

Choose a reason for hiding this comment

The 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);
}
Expand Down Expand Up @@ -169,6 +158,18 @@ public Path get() {
return defaultDir;
}

public synchronized void setCustomValuesForObject(Path path, String info, Boolean requireExists){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing code all has a space between ) and {.

Suggested change
public synchronized void setCustomValuesForObject(Path path, String info, Boolean requireExists){
public synchronized void setCustomValuesForObject(Path path, String info, Boolean requireExists) {

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();
}
}
Copy link

Choose a reason for hiding this comment

The 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();
Expand Down
2 changes: 1 addition & 1 deletion src/chatty/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void uncaughtException(Thread t, Throwable e) {
return;
}
try {
String stacktrace = Logging.getStacktrace(e);
String stacktrace = StackTrace.getStacktrace(e);
if (t != null && t.getName() != null && t.getName().startsWith("JKeyMaster-")) {
// Output as warning, but also show directly to user
LOGGER.warning(String.format("[%s/%s][%s][%s]\n%s",
Expand Down
270 changes: 181 additions & 89 deletions src/chatty/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The 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){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one other place a HashMap<> is used in the code it uses a space to separate the types.

Also ){ again.

Suggested change
private static HashMap<String,Boolean> setPossibleChannelType(String[] catSplit, HashMap<String,Boolean> channelType){
private static HashMap<String, Boolean> setPossibleChannelType(String[] catSplit, HashMap<String, Boolean> channelType) {


Copy link

Choose a reason for hiding this comment

The 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);
Copy link

Choose a reason for hiding this comment

The 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){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap<> + ){

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){
Copy link

Choose a reason for hiding this comment

The 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<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap<>

for(String channelType: channelTypes){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. In existing code for and its ( are separated by a space.
  2. In existing code that you’re replacing the Type variable : class uses :.
  3. ){ as mentioned elsewhere.
Suggested change
for(String channelType: channelTypes){
for (String channelType : channelTypes) {

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);
}
}
}
Copy link

Choose a reason for hiding this comment

The 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<>();
Expand All @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter spaces.

Suggested change
getPossibleChannelNames(channel,prepend,result);
getPossibleChannelNames(channel, prepend, result);

}
}
return result;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
20 changes: 2 additions & 18 deletions src/chatty/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public String format(LogRecord record) {
record.getSourceClassName(),
record.getSourceMethodName(),
record.getLevel().getName(),
getStacktraceForLogging(record.getThrown()));
StackTrace.getStacktraceForLogging(record.getThrown()));
}

}
Expand All @@ -137,7 +137,7 @@ public static String formatRecordCompact(LogRecord record) {
simpleFormatMessage(record),
record.getSourceClassName(),
record.getSourceMethodName(),
getStacktraceForLogging(record.getThrown()));
StackTrace.getStacktraceForLogging(record.getThrown()));
}

private static String simpleFormatMessage(LogRecord record) {
Expand All @@ -154,22 +154,6 @@ private static String simpleFormatMessage(LogRecord record) {
}
}

public static String getStacktrace(Throwable t) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString();
}

public static String getStacktraceForLogging(Throwable t) {
if (t != null) {
try {
return "\n:"+getStacktrace(t);
} catch (Exception ex) {
return "\n:Error getting stacktrace";
}
}
return "";
}

static class FileFilter implements Filter {

Expand Down
Loading