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

refact: clean code & typo & update the name of getTimeZone #105

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ body:
options:
- exception / error (异常报错)
- logic (逻辑设计问题)
- performence (性能下降)
- performance (性能下降)
- others (please edit later)

- type: checkboxes
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/question_ask.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:
label: Problem Type (问题类型)
options:
- struct / logic (架构 / 逻辑设计问题)
- performence (性能优化)
- performance (性能优化)
- exception / error (异常报错)
- others (please edit later)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ public final List<Lock> lockAll(Object... keys) {
Lock lock = this.locks.get(key);
locks.add(lock);
}
Collections.sort(locks, (a, b) -> {
locks.sort((a, b) -> {
int diff = a.hashCode() - b.hashCode();
if (diff == 0 && a != b) {
diff = this.indexOf(a) - this.indexOf(b);
assert diff != 0;
}
return diff;
});
for (int i = 0; i < locks.size(); i++) {
locks.get(i).lock();
for (Lock lock : locks) {
lock.lock();
}
return Collections.unmodifiableList(locks);
}
Expand Down Expand Up @@ -125,8 +125,8 @@ public List<Lock> lockAll(Object key1, Object key2) {
ImmutableList.of(lock2, lock1) :
ImmutableList.of(lock1, lock2);

for (int i = 0; i < locks.size(); i++) {
locks.get(i).lock();
for (Lock lock : locks) {
lock.lock();
}

return locks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@

public class PausableScheduledThreadPool extends ScheduledThreadPoolExecutor {

private static final Logger LOG = Log.logger(
PausableScheduledThreadPool.class);
private static final Logger LOG = Log.logger(PausableScheduledThreadPool.class);

private volatile boolean paused = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Nullable;
Expand All @@ -32,88 +33,55 @@
public final class OptionChecker {

public static <O> Predicate<O> disallowEmpty() {
return new Predicate<O>() {
@Override
public boolean apply(@Nullable O o) {
if (o == null) {
return false;
}
if (o instanceof String) {
return StringUtils.isNotBlank((String) o);
}
if (o.getClass().isArray() && (Array.getLength(o) == 0)) {
return false;
}
if (o instanceof Iterable &&
!((Iterable<?>) o).iterator().hasNext()) {
return false;
}
return true;
return o -> {
if (o == null) {
return false;
}
if (o instanceof String) {
return StringUtils.isNotBlank((String) o);
}
if (o.getClass().isArray() && (Array.getLength(o) == 0)) {
return false;
}
return !(o instanceof Iterable) || ((Iterable<?>) o).iterator().hasNext();
};
}

@SuppressWarnings("unchecked")
public static <O> Predicate<O> allowValues(O... values) {
return new Predicate<O>() {
@Override
public boolean apply(@Nullable O o) {
return o != null && Arrays.asList(values).contains(o);
}
};
return o -> o != null && Arrays.asList(values).contains(o);
}

@SuppressWarnings("unchecked")
public static <O> Predicate<List<O>> inValues(O... values) {
return new Predicate<List<O>>() {
@Override
public boolean apply(@Nullable List<O> o) {
return o != null && Arrays.asList(values).containsAll(o);
}
};
return o -> o != null && new HashSet<>(Arrays.asList(values)).containsAll(o);
}

public static <N extends Number> Predicate<N> positiveInt() {
return new Predicate<N>() {
@Override
public boolean apply(@Nullable N number) {
return number != null && number.longValue() > 0;
}
};
return number -> number != null && number.longValue() > 0;
}

public static <N extends Number> Predicate<N> nonNegativeInt() {
return new Predicate<N>() {
@Override
public boolean apply(@Nullable N number) {
return number != null && number.longValue() >= 0;
}
};
return number -> number != null && number.longValue() >= 0;
}

public static <N extends Number> Predicate<N> rangeInt(N min, N max) {
return new Predicate<N>() {
@Override
public boolean apply(@Nullable N number) {
if (number == null) {
return false;
}
long value = number.longValue();
return value >= min.longValue() && value <= max.longValue();
return number -> {
if (number == null) {
return false;
}
long value = number.longValue();
return value >= min.longValue() && value <= max.longValue();
};
}

public static <N extends Number> Predicate<N> rangeDouble(N min, N max) {
return new Predicate<N>() {
@Override
public boolean apply(@Nullable N number) {
if (number == null) {
return false;
}
double value = number.doubleValue();
return value >= min.doubleValue() && value <= max.doubleValue();
return number -> {
if (number == null) {
return false;
}
double value = number.doubleValue();
return value >= min.doubleValue() && value <= max.doubleValue();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public synchronized void setTimeZone(String zoneId) {
this.formatter = this.formatter.withZone(zone);
}

public TimeZone getTimeZome() {
public TimeZone getTimeZone() {
return this.formatter.getZone().toTimeZone();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public Future<Integer> notify(String event, @Nullable Object... args) {
try {
all.next().event(ev);
count++;
} catch (Throwable ignored) {
LOG.warn("Failed to handle event: {}", ev, ignored);
} catch (Throwable e) {
LOG.warn("Failed to handle event: {}", ev, e);
}
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public interface EventListener extends java.util.EventListener {
* @param event object
* @return event result
*/
public Object event(Event event);
Object event(Event event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
package org.apache.hugegraph.func;

public interface TriFunction <T1, T2, T3, R> {
public R apply(T1 v1, T2 v2, T3 v3);

R apply(T1 v1, T2 v2, T3 v3);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected boolean fetch() {
return true;
}

Iterator<T> first = null;
Iterator<T> first;
while ((first = this.itors.peekFirst()) != null && !first.hasNext()) {
if (first == this.itors.peekLast() && this.itors.size() == 1) {
this.currentIterator = first;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

public interface Metadatable {

public Object metadata(String meta, Object... args);
Object metadata(String meta, Object... args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

public interface LicenseManager {

public LicenseParams installLicense() throws Exception;
LicenseParams installLicense() throws Exception;

public void uninstallLicense() throws Exception;
void uninstallLicense() throws Exception;

public LicenseParams verifyLicense() throws Exception;
LicenseParams verifyLicense() throws Exception;

public interface VerifyCallback {
interface VerifyCallback {

public void onVerifyLicense(LicenseParams params) throws Exception;
void onVerifyLicense(LicenseParams params) throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ public long totalChildrenWasted() {
@Override
public void fillChildrenTotal(List<Stopwatch> children) {
// Fill total times of children
this.totalChildrenTimes = children.stream().mapToLong(
c -> c.totalTimes()).sum();
this.totalChildrenTimes = children.stream().mapToLong(Stopwatch::totalTimes).sum();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,9 @@ public long totalChildrenWasted() {
@Override
public void fillChildrenTotal(List<Stopwatch> children) {
// Fill total wasted cost of children
this.totalChildrenWasted = children.stream().mapToLong(
c -> c.totalWasted()).sum();
this.totalChildrenWasted = children.stream().mapToLong(Stopwatch::totalWasted).sum();
// Fill total times of children
this.totalChildrenTimes = children.stream().mapToLong(
c -> c.totalTimes()).sum();
this.totalChildrenTimes = children.stream().mapToLong(Stopwatch::totalTimes).sum();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ public void clear() {

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR })
public static @interface Watched {
public String value() default "";
public String prefix() default "";
public @interface Watched {
String value() default "";
String prefix() default "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,40 @@

public interface Stopwatch extends Cloneable {

public Path id();
public String name();
public Path parent();
Path id();
String name();
Path parent();

public void startTime(long startTime);
public void endTime(long startTime);
void startTime(long startTime);
void endTime(long startTime);

public void lastStartTime(long startTime);
void lastStartTime(long startTime);

public long times();
public long totalTimes();
public long totalChildrenTimes();
long times();
long totalTimes();
long totalChildrenTimes();

public long totalCost();
public void totalCost(long otherCost);
long totalCost();
void totalCost(long otherCost);

public long minCost();
public long maxCost();
long minCost();
long maxCost();

public long totalWasted();
public long totalSelfWasted();
public long totalChildrenWasted();
long totalWasted();
long totalSelfWasted();
long totalChildrenWasted();

public void fillChildrenTotal(List<Stopwatch> children);
void fillChildrenTotal(List<Stopwatch> children);

public Stopwatch copy();
Stopwatch copy();

public Stopwatch child(String name);
public Stopwatch child(String name, Stopwatch watch);
Stopwatch child(String name);
Stopwatch child(String name, Stopwatch watch);

public boolean empty();
public void clear();
boolean empty();
void clear();

public default String toJson() {
default String toJson() {
int len = 200 + this.name().length() + this.parent().length();
StringBuilder sb = new StringBuilder(len);
sb.append("{");
Expand All @@ -75,14 +75,14 @@ public default String toJson() {
return sb.toString();
}

public static Path id(Path parent, String name) {
static Path id(Path parent, String name) {
if (parent == Path.EMPTY && name == Path.ROOT_NAME) {
return Path.EMPTY;
}
return new Path(parent, name);
}

public static final class Path implements Comparable<Path> {
final class Path implements Comparable<Path> {

public static final String ROOT_NAME = "root";
public static final Path EMPTY = new Path("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ private static PoolingHttpClientConnectionManager connectionManager(
String url,
ClientConfig conf) {
String protocol = (String) conf.getProperty("protocol");
if (protocol == null || protocol.equals("http")) {
if (protocol == null || "http".equals(protocol)) {
return new PoolingHttpClientConnectionManager(TTL, TimeUnit.HOURS);
}

assert protocol.equals("https");
assert "https".equals(protocol);
String trustStoreFile = (String) conf.getProperty("trustStoreFile");
E.checkArgument(trustStoreFile != null && !trustStoreFile.isEmpty(),
"The trust store file must be set when use https");
Expand Down
Loading