From 416469c4cb7d9ce820c3b2c0bdfeb2077f994042 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 21 May 2008 20:52:14 +0000 Subject: [PATCH] simplify abstract bean a bit git-svn-id: http://trooper/svn/projects/jSite/trunk@925 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/jsite/core/Node.java | 12 ++------ src/net/pterodactylus/jsite/core/Request.java | 36 ++++++---------------- src/net/pterodactylus/jsite/project/Project.java | 18 +++-------- src/net/pterodactylus/util/beans/AbstractBean.java | 22 ++++++++++++- 4 files changed, 38 insertions(+), 50 deletions(-) diff --git a/src/net/pterodactylus/jsite/core/Node.java b/src/net/pterodactylus/jsite/core/Node.java index 9435995..b7bf637 100644 --- a/src/net/pterodactylus/jsite/core/Node.java +++ b/src/net/pterodactylus/jsite/core/Node.java @@ -72,9 +72,7 @@ public class Node extends AbstractBean { public void setName(String name) { String oldName = this.name; this.name = name; - if (!equal(oldName, name)) { - firePropertyChange(PROPERTY_NAME, oldName, name); - } + fireIfPropertyChanged(PROPERTY_NAME, oldName, name); } /** @@ -95,9 +93,7 @@ public class Node extends AbstractBean { public void setHostname(String hostname) { String oldHostname = this.hostname; this.hostname = hostname; - if (!equal(oldHostname, hostname)) { - firePropertyChange(PROPERTY_HOSTNAME, oldHostname, hostname); - } + fireIfPropertyChanged(PROPERTY_HOSTNAME, oldHostname, hostname); } /** @@ -118,9 +114,7 @@ public class Node extends AbstractBean { public void setPort(int port) { int oldPort = this.port; this.port = port; - if (oldPort != port) { - firePropertyChange(PROPERTY_PORT, oldPort, port); - } + fireIfPropertyChanged(PROPERTY_PORT, oldPort, port); } /** diff --git a/src/net/pterodactylus/jsite/core/Request.java b/src/net/pterodactylus/jsite/core/Request.java index 7b1bbe9..82df6f8 100644 --- a/src/net/pterodactylus/jsite/core/Request.java +++ b/src/net/pterodactylus/jsite/core/Request.java @@ -19,7 +19,6 @@ package net.pterodactylus.jsite.core; - import net.pterodactylus.util.beans.AbstractBean; /** @@ -28,8 +27,7 @@ import net.pterodactylus.util.beans.AbstractBean; * @author David ‘Bombe’ Roden <bombe@freenetproject.org> * @version $Id$ */ -public class Request extends -AbstractBean { +public class Request extends AbstractBean { /** * The type of a request. @@ -162,9 +160,7 @@ AbstractBean { public void setType(Type type) { Type oldType = this.type; this.type = type; - if (!equal(oldType, type)) { - firePropertyChange(PROPERTY_TYPE, oldType, type); - } + fireIfPropertyChanged(PROPERTY_TYPE, oldType, type); } /** @@ -185,9 +181,7 @@ AbstractBean { public void setClientToken(String clientToken) { String oldClientToken = this.clientToken; this.clientToken = clientToken; - if (!equal(oldClientToken, clientToken)) { - firePropertyChange(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken); - } + fireIfPropertyChanged(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken); } /** @@ -210,9 +204,7 @@ AbstractBean { public void setTotalBlocks(int totalBlocks) { int oldTotalBlocks = this.totalBlocks; this.totalBlocks = totalBlocks; - if (oldTotalBlocks != totalBlocks) { - firePropertyChange(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks); - } + fireIfPropertyChanged(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks); } /** @@ -229,9 +221,7 @@ AbstractBean { public void setRequiredBlocks(int requiredBlocks) { int oldRequiredBlocks = this.requiredBlocks; this.requiredBlocks = requiredBlocks; - if (oldRequiredBlocks != requiredBlocks) { - firePropertyChange(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks); - } + fireIfPropertyChanged(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks); } /** @@ -248,9 +238,7 @@ AbstractBean { public void setSuccessfulBlocks(int successfulBlocks) { int oldSuccessfulBlocks = this.successfulBlocks; this.successfulBlocks = successfulBlocks; - if (oldSuccessfulBlocks != successfulBlocks) { - firePropertyChange(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks); - } + fireIfPropertyChanged(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks); } /** @@ -267,9 +255,7 @@ AbstractBean { public void setFailedBlocks(int failedBlocks) { int oldFailedBlocks = this.failedBlocks; this.failedBlocks = failedBlocks; - if (oldFailedBlocks != failedBlocks) { - firePropertyChange(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks); - } + fireIfPropertyChanged(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks); } /** @@ -286,9 +272,7 @@ AbstractBean { public void setFatallyFailedBlocks(int fatallyFailedBlocks) { int oldFatallyFailedBlocks = this.fatallyFailedBlocks; this.fatallyFailedBlocks = fatallyFailedBlocks; - if (oldFatallyFailedBlocks != fatallyFailedBlocks) { - firePropertyChange(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks); - } + fireIfPropertyChanged(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks); } /** @@ -305,9 +289,7 @@ AbstractBean { public void setTotalFinalized(boolean totalFinalized) { boolean oldTotalFinalized = this.totalFinalized; this.totalFinalized = totalFinalized; - if (oldTotalFinalized != totalFinalized) { - firePropertyChange(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized); - } + fireIfPropertyChanged(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized); } } diff --git a/src/net/pterodactylus/jsite/project/Project.java b/src/net/pterodactylus/jsite/project/Project.java index 2c6609f..4025b82 100644 --- a/src/net/pterodactylus/jsite/project/Project.java +++ b/src/net/pterodactylus/jsite/project/Project.java @@ -84,9 +84,7 @@ public class Project extends AbstractBean { public void setName(String name) { String oldName = this.name; this.name = name; - if (!equal(oldName, name)) { - firePropertyChange(PROPERTY_NAME, oldName, name); - } + fireIfPropertyChanged(PROPERTY_NAME, oldName, name); } /** @@ -107,9 +105,7 @@ public class Project extends AbstractBean { public void setDescription(String description) { String oldDescription = this.description; this.description = description; - if (!equal(oldDescription, description)) { - firePropertyChange(PROPERTY_DESCRIPTION, oldDescription, description); - } + fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description); } /** @@ -130,9 +126,7 @@ public class Project extends AbstractBean { public void setPublicKey(String publicKey) { String oldPublicKey = this.publicKey; this.publicKey = publicKey; - if (!equal(oldPublicKey, publicKey)) { - firePropertyChange(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey); - } + fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey); } /** @@ -153,9 +147,7 @@ public class Project extends AbstractBean { public void setPrivateKey(String privateKey) { String oldPrivateKey = this.privateKey; this.privateKey = privateKey; - if (!equal(oldPrivateKey, privateKey)) { - firePropertyChange(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey); - } + fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey); } /** @@ -176,7 +168,7 @@ public class Project extends AbstractBean { public void setBasePath(String basePath) { String oldBasePath = this.basePath; this.basePath = basePath; - firePropertyChange(PROPERTY_LOCAL_PATH, oldBasePath, basePath); + fireIfPropertyChanged(PROPERTY_LOCAL_PATH, oldBasePath, basePath); } } diff --git a/src/net/pterodactylus/util/beans/AbstractBean.java b/src/net/pterodactylus/util/beans/AbstractBean.java index 53fe690..b586353 100644 --- a/src/net/pterodactylus/util/beans/AbstractBean.java +++ b/src/net/pterodactylus/util/beans/AbstractBean.java @@ -75,6 +75,26 @@ public abstract class AbstractBean { } /** + * Fires a property change event if the two values are not equal. + * + * @param propertyName + * The name of the property + * @param oldValue + * The old value of the property + * @param newValue + * The new value of the property + */ + protected void fireIfPropertyChanged(String propertyName, Object oldValue, Object newValue) { + if (!equal(oldValue, newValue)) { + firePropertyChange(propertyName, oldValue, newValue); + } + } + + // + // PRIVATE METHODS + // + + /** * Compares the two objects and returns whether they are equal according to * {@link Object#equals(Object)}. This method takes null * into account as a valid value for an object. @@ -86,7 +106,7 @@ public abstract class AbstractBean { * @return true if the two objects are equal, * false otherwise */ - protected boolean equal(Object first, Object second) { + private boolean equal(Object first, Object second) { return ((first == null) && (second == null)) || ((first != null) && first.equals(second)) || ((second != null) && second.equals(first)); } -- 2.7.4