whitespace fixups
[jSite2.git] / src / net / pterodactylus / jsite / core / Request.java
index e3240b5..a61bb94 100644 (file)
 
 package net.pterodactylus.jsite.core;
 
-import java.beans.PropertyChangeEvent;
-import java.beans.PropertyChangeListener;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import net.pterodactylus.util.beans.AbstractBean;
 
 /**
  * A request is an ongoing download or upload reported by the freenet node.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
  */
-public class Request {
+public class Request extends AbstractBean {
 
        /**
         * The type of a request.
-        * 
+        *
         * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
-        * @version $Id$
         */
        public enum Type {
 
@@ -61,6 +55,18 @@ public class Request {
        /** Name of the “client token” property. */
        public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
 
+       /** Name of the “finished” property. */
+       public static final String PROPERTY_FINISHED = "finished";
+
+       /** Name of the “successful” property. */
+       public static final String PROPERTY_SUCCESSFUL = "successful";
+
+       /** Name of the “fetchable” property. */
+       public static final String PROPERTY_FETCHABLE = "fetchable";
+
+       /** Name of the “URI” property. */
+       public static final String PROPERTY_URI = "uri";
+
        /** Name of the “total blocks” property. */
        public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
 
@@ -79,9 +85,6 @@ public class Request {
        /** Name of the “total finalized” property. */
        public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
 
-       /** Property change listeners. */
-       private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
-
        /** The node the request belongs to. */
        private final Node node;
 
@@ -94,6 +97,18 @@ public class Request {
        /** The client token of the request. */
        private String clientToken;
 
+       /** Whether the request is finished. */
+       private boolean finished;
+
+       /** Whether the request was finished successfully. */
+       private boolean successful;
+
+       /** Whether the data is already fetchable (in case of put requests). */
+       private boolean fetchable;
+
+       /** The generated URI. */
+       private String uri;
+
        /** The total number of blocks. */
        private int totalBlocks;
 
@@ -114,7 +129,7 @@ public class Request {
 
        /**
         * Creates a new request with the given identifier.
-        * 
+        *
         * @param node
         *            The node the request belongs to
         * @param identifier
@@ -130,49 +145,8 @@ public class Request {
        //
 
        /**
-        * Adds a property change listener.
-        * 
-        * @param propertyChangeListener
-        *            The property change listener to add
-        */
-       public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
-               propertyChangeListeners.add(propertyChangeListener);
-       }
-
-       /**
-        * Removes a property change listener.
-        * 
-        * @param propertyChangeListener
-        *            The property change listener to remove
-        */
-       public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
-               propertyChangeListeners.remove(propertyChangeListener);
-       }
-
-       /**
-        * Notifies all listeners that a property has changed.
-        * 
-        * @param property
-        *            The name of the property
-        * @param oldValue
-        *            The old value of the property
-        * @param newValue
-        *            The new value of the property
-        */
-       private void firePropertyChange(String property, Object oldValue, Object newValue) {
-               PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
-               for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
-                       propertyChangeListener.propertyChange(propertyChangeEvent);
-               }
-       }
-
-       //
-       // ACCESSORS
-       //
-
-       /**
         * Returns the node the request belongs to.
-        * 
+        *
         * @return The node the request belongs to
         */
        public Node getNode() {
@@ -181,7 +155,7 @@ public class Request {
 
        /**
         * Returns the identifier of the request. It is unique per node.
-        * 
+        *
         * @return The identifier of the request
         */
        public String getIdentifier() {
@@ -190,7 +164,7 @@ public class Request {
 
        /**
         * Returns the type of the request.
-        * 
+        *
         * @return The type of the request
         */
 
@@ -200,22 +174,19 @@ public class Request {
 
        /**
         * Sets the type of the request.
-        * 
+        *
         * @param type
         *            The type of the request
         */
-
-       public void setType(Type type) {
+       void setType(Type type) {
                Type oldType = this.type;
                this.type = type;
-               if (((oldType == null) && (type != null)) || ((oldType != null) && (type == null)) || ((type != null) && !type.equals(oldType))) {
-                       firePropertyChange(PROPERTY_TYPE, oldType, type);
-               }
+               fireIfPropertyChanged(PROPERTY_TYPE, oldType, type);
        }
 
        /**
         * Returns the client token of the request.
-        * 
+        *
         * @return The client token of the request
         */
        public String getClientToken() {
@@ -224,23 +195,113 @@ public class Request {
 
        /**
         * Sets the client token of the request.
-        * 
+        *
         * @param clientToken
         *            The client token of the request
         */
-       public void setClientToken(String clientToken) {
+       void setClientToken(String clientToken) {
                String oldClientToken = this.clientToken;
                this.clientToken = clientToken;
-               if (((oldClientToken == null) && (clientToken != null)) || ((oldClientToken != null) && (clientToken == null)) || ((clientToken != null) && !clientToken.equals(oldClientToken))) {
-                       firePropertyChange(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken);
-               }
+               fireIfPropertyChanged(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken);
+       }
+
+       /**
+        * Returns whether the request has finished.
+        *
+        * @see #isSuccessful()
+        * @return <code>true</code> if the request is finished,
+        *         <code>false</code> otherwise
+        */
+       public boolean isFinished() {
+               return finished;
+       }
+
+       /**
+        * Sets whether the request has finished.
+        *
+        * @param finished
+        *            <code>true</code> if the request has finished,
+        *            <code>false</code> otherwise
+        */
+       void setFinished(boolean finished) {
+               boolean oldFinished = this.finished;
+               this.finished = finished;
+               firePropertyChange(PROPERTY_FINISHED, oldFinished, finished);
+       }
+
+       /**
+        * Returns whether the request finished successfully. This value will only
+        * have meaning if {@link #isFinished()} returns <code>true</code>.
+        *
+        * @return <code>true</code> if the request finished successfully,
+        *         <code>false</code> otherwise
+        */
+       public boolean isSuccessful() {
+               return successful;
+       }
+
+       /**
+        * Sets whether this request finished successfully.
+        *
+        * @param successful
+        *            <code>true</code> if the request finished successfully,
+        *            <code>false</code> otherwise
+        */
+       void setSuccessful(boolean successful) {
+               boolean oldSuccessful = this.successful;
+               this.successful = successful;
+               firePropertyChange(PROPERTY_SUCCESSFUL, oldSuccessful, successful);
+       }
+
+       /**
+        * Returns whether the data inserted by this {@link Type#put} or
+        * {@link Type#putDir} request is already fetchable by other clients.
+        *
+        * @return <code>true</code> if the data is already fetchable,
+        *         <code>false</code> otherwise
+        */
+       public boolean isFetchable() {
+               return fetchable;
+       }
+
+       /**
+        * Sets whether the data inserted by this {@link Type#put} or
+        * {@link Type#putDir} request is already fetchable by other clients.
+        *
+        * @param fetchable
+        *            <code>true</code> if the data is already fetchable,
+        *            <code>false</code> otherwise
+        */
+       void setFetchable(boolean fetchable) {
+               boolean oldFetchable = this.fetchable;
+               this.fetchable = fetchable;
+               firePropertyChange(PROPERTY_FETCHABLE, oldFetchable, fetchable);
+       }
+
+       /**
+        * Returns the URI generated by this request.
+        *
+        * @return The generated URI
+        */
+       public String getURI() {
+               return uri;
+       }
+
+       /**
+        * Sets the URI generated by this request.
+        *
+        * @param uri
+        *            The generated URI
+        */
+       void setURI(String uri) {
+               this.uri = uri;
        }
 
        /**
         * Returns the total number of blocks of a request. Until
         * {@link #isTotalFinalized()} returns <code>true</code> this value may
         * change!
-        * 
+        *
         * @return The total number of blocks of a request
         */
        public int getTotalBlocks() {
@@ -249,16 +310,14 @@ public class Request {
 
        /**
         * Sets the total number of blocks of a request.
-        * 
+        *
         * @param totalBlocks
         *            The total number of blocks
         */
-       public void setTotalBlocks(int totalBlocks) {
+       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);
        }
 
        /**
@@ -272,12 +331,10 @@ public class Request {
         * @param requiredBlocks
         *            the requiredBlocks to set
         */
-       public void setRequiredBlocks(int requiredBlocks) {
+       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);
        }
 
        /**
@@ -291,12 +348,10 @@ public class Request {
         * @param successfulBlocks
         *            the successfulBlocks to set
         */
-       public void setSuccessfulBlocks(int successfulBlocks) {
+       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);
        }
 
        /**
@@ -310,12 +365,10 @@ public class Request {
         * @param failedBlocks
         *            the failedBlocks to set
         */
-       public void setFailedBlocks(int failedBlocks) {
+       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);
        }
 
        /**
@@ -329,12 +382,10 @@ public class Request {
         * @param fatallyFailedBlocks
         *            the fatallyFailedBlocks to set
         */
-       public void setFatallyFailedBlocks(int fatallyFailedBlocks) {
+       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);
        }
 
        /**
@@ -348,12 +399,10 @@ public class Request {
         * @param totalFinalized
         *            the totalFinalized to set
         */
-       public void setTotalFinalized(boolean totalFinalized) {
+       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);
        }
 
 }