ToStringBuilder

This commit is contained in:
huanghongxun 2018-02-20 12:11:31 +08:00
parent 602114af55
commit 95a509b86c
6 changed files with 55 additions and 5 deletions

View File

@ -318,18 +318,33 @@ public final class FXUtils {
checkBox.selectedProperty().unbindBidirectional(property);
}
/**
* Bind combo box selection with given enum property bidirectionally.
* You should <b>only and always</b> use {@code bindEnum} as well as {@code unbindEnum} at the same time.
* @param comboBox the combo box being bound with {@code property}.
* @param property the property being bound with {@code combo box}.
* @see #unbindEnum(JFXComboBox)
*/
@SuppressWarnings("unchecked")
public static void bindEnum(JFXComboBox<?> comboBox, Property<? extends Enum> property) {
unbindEnum(comboBox);
ChangeListener<Number> listener = (a, b, newValue) -> {
((Property) property).setValue(property.getValue().getClass().getEnumConstants()[newValue.intValue()]);
};
comboBox.getSelectionModel().select(property.getValue().ordinal());
comboBox.getProperties().put("listener", listener);
comboBox.getProperties().put("FXUtils.bindEnum.listener", listener);
comboBox.getSelectionModel().selectedIndexProperty().addListener(listener);
}
/**
* Unbind combo box selection with given enum property bidirectionally.
* You should <b>only and always</b> use {@code bindEnum} as well as {@code unbindEnum} at the same time.
* @param comboBox the combo box being bound with the property which can be inferred by {@code bindEnum}.
* @see #bindEnum(JFXComboBox, Property)
*/
@SuppressWarnings("unchecked")
public static void unbindEnum(JFXComboBox<?> comboBox) {
ChangeListener listener = Lang.get(comboBox.getProperties(), "listener", ChangeListener.class).orElse(null);
ChangeListener listener = Lang.get(comboBox.getProperties(), "FXUtils.bindEnum.listener", ChangeListener.class).orElse(null);
if (listener == null) return;
comboBox.getSelectionModel().selectedIndexProperty().removeListener(listener);
}

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.game;
import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.ToStringBuilder;
import java.util.Collections;
import java.util.HashMap;
@ -52,4 +53,8 @@ public final class AssetIndex {
return Collections.unmodifiableMap(objects);
}
@Override
public String toString() {
return new ToStringBuilder(this).append("virtual", virtual).append("objects", objects).toString();
}
}

View File

@ -21,6 +21,7 @@ import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.ToStringBuilder;
import org.jackhuang.hmcl.util.Validation;
/**
@ -67,6 +68,11 @@ public class DownloadInfo implements Validation {
return size;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("url", url).append("sha1", sha1).append("size", size).toString();
}
@Override
public void validate() throws JsonParseException {
if (StringUtils.isBlank(url))

View File

@ -32,7 +32,7 @@ public final class ExtractRules {
private final List<String> exclude;
public ExtractRules() {
this.exclude = Collections.EMPTY_LIST;
this.exclude = Collections.emptyList();
}
public ExtractRules(List<String> exclude) {
@ -44,7 +44,7 @@ public final class ExtractRules {
}
public boolean shouldExtract(String path) {
return exclude.stream().noneMatch(it -> path.startsWith(it));
return exclude.stream().noneMatch(path::startsWith);
}
}

View File

@ -22,6 +22,7 @@ import com.google.gson.reflect.TypeToken;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.OperatingSystem;
import org.jackhuang.hmcl.util.Platform;
import org.jackhuang.hmcl.util.ToStringBuilder;
import java.lang.reflect.Type;
import java.util.List;
@ -142,7 +143,7 @@ public class Library implements Comparable<Library> {
@Override
public String toString() {
return "Library[" + getName() + "]";
return new ToStringBuilder(this).append("name", getName()).toString();
}
@Override

View File

@ -0,0 +1,23 @@
package org.jackhuang.hmcl.util;
public class ToStringBuilder {
private final StringBuilder stringBuilder;
private boolean first = true;
public ToStringBuilder(Object object) {
stringBuilder = new StringBuilder(object.getClass().getSimpleName()).append(" [");
}
public ToStringBuilder append(String name, Object content) {
if (!first)
stringBuilder.append(", ");
stringBuilder.append(name).append('=').append(content.toString());
return this;
}
@Override
public String toString() {
return stringBuilder.toString() + "]";
}
}