Skip to content

Instantly share code, notes, and snippets.

@rnaufal
Created February 25, 2020 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnaufal/cfaadb809840c2408297a9c44e948287 to your computer and use it in GitHub Desktop.
Save rnaufal/cfaadb809840c2408297a9c44e948287 to your computer and use it in GitHub Desktop.
public class CharSummaryStatistics {
private long totalDigits = 0;
private long totalUppercaseChars = 0;
private long totalLowercaseChars = 0;
private long totalInvalidChars = 0;
public CharSummaryStatistics() {
}
public CharSummaryStatistics(final long totalDigits,
final long totalUppercaseChars,
final long totalLowercaseChars,
final long totalInvalidChars) {
this.totalDigits = totalDigits;
this.totalUppercaseChars = totalUppercaseChars;
this.totalLowercaseChars = totalLowercaseChars;
this.totalInvalidChars = totalInvalidChars;
}
public void accept(final int currentChar) {
if (Character.isDigit(currentChar)) {
totalDigits += 1;
} else if (Character.isUpperCase(currentChar)) {
totalUppercaseChars += 1;
} else if (Character.isLowerCase(currentChar)) {
totalLowercaseChars += 1;
} else {
totalInvalidChars += 1;
}
}
public void combine(final CharSummaryStatistics other) {
this.totalDigits += other.totalDigits;
this.totalUppercaseChars += other.totalUppercaseChars;
this.totalLowercaseChars += other.totalLowercaseChars;
}
// getters and setters omitted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment