Add default constructor that creates a random ID.
[jSite2.git] / src / net / pterodactylus / jsite / core / Request.java
index e3240b5..15dae81 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * jSite2 - Request.java -
- * Copyright © 2008 David Roden
+ * jSite-next - Request.java -
+ * Copyright © 2009 David Roden
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 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.jsite.util.IdGenerator;
+import net.pterodactylus.util.number.Hex;
 
 /**
- * A request is an ongoing download or upload reported by the freenet node.
- * 
- * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
+ * Wraps a request that is executed by the Freenet node.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class Request {
 
-       /**
-        * The type of a request.
-        * 
-        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
-        * @version $Id$
-        */
-       public enum Type {
-
-               /** Type of request is unknown. */
-               unknown,
-
-               /** The request is a Get request. */
-               get,
-
-               /** The request is a Put request. */
-               put,
-
-               /** The request is a PutDir request. */
-               putDir
-
-       }
-
-       /** Name of the “type” property. */
-       public static final String PROPERTY_TYPE = "type";
-
-       /** Name of the “client token” property. */
-       public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
-
-       /** Name of the “total blocks” property. */
-       public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
-
-       /** Name of the “required blocks” property. */
-       public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
-
-       /** Name of the “successful blocks” property. */
-       public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
-
-       /** Name of the “failed blocks” property. */
-       public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
-
-       /** Name of the “fatally failed blocks” property. */
-       public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
-
-       /** 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;
-
-       /** The identifier of the request. */
-       private final String identifier;
-
-       /** The type of the request. */
-       private Type type;
+       /** The ID of the request. */
+       private final String id;
 
        /** The client token of the request. */
        private String clientToken;
 
-       /** The total number of blocks. */
-       private int totalBlocks;
-
-       /** The required number of blocks. */
-       private int requiredBlocks;
-
-       /** The number of successful blocks. */
-       private int successfulBlocks;
-
-       /** The number of failedBlocks. */
-       private int failedBlocks;
-
-       /** The number of fatally failed blocks. */
-       private int fatallyFailedBlocks;
-
-       /** Whether the total number has been finalized. */
-       private boolean totalFinalized;
-
        /**
-        * Creates a new request with the given identifier.
-        * 
-        * @param node
-        *            The node the request belongs to
-        * @param identifier
-        *            The identifier of the request
+        * Creates a new request with a random ID.
         */
-       Request(Node node, String identifier) {
-               this.node = node;
-               this.identifier = identifier;
+       public Request() {
+               this(Hex.toHex(IdGenerator.generateId()));
        }
 
-       //
-       // EVENT MANAGEMENT
-       //
-
        /**
-        * Adds a property change listener.
-        * 
-        * @param propertyChangeListener
-        *            The property change listener to add
+        * Creates a new request.
+        *
+        * @param id
+        *            The ID of the request
         */
-       public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
-               propertyChangeListeners.add(propertyChangeListener);
+       public Request(String id) {
+               this.id = id;
        }
 
        /**
-        * Removes a property change listener.
-        * 
-        * @param propertyChangeListener
-        *            The property change listener to remove
+        * Returns the ID of the request.
+        *
+        * @return The request’s ID
         */
-       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() {
-               return node;
-       }
-
-       /**
-        * Returns the identifier of the request. It is unique per node.
-        * 
-        * @return The identifier of the request
-        */
-       public String getIdentifier() {
-               return identifier;
-       }
-
-       /**
-        * Returns the type of the request.
-        * 
-        * @return The type of the request
-        */
-
-       public Type getType() {
-               return type;
-       }
-
-       /**
-        * Sets the type of the request.
-        * 
-        * @param type
-        *            The type of the request
-        */
-
-       public 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);
-               }
+       public String getId() {
+               return id;
        }
 
        /**
         * Returns the client token of the request.
-        * 
-        * @return The client token of the request
+        *
+        * @return The request’s client token
         */
        public String getClientToken() {
                return clientToken;
        }
 
        /**
-        * Sets the client token of the request.
-        * 
+        * Sets the client token of the request
+        *
         * @param clientToken
-        *            The client token of the request
+        *            The request’s new client token
         */
        public 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);
-               }
-       }
-
-       /**
-        * 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() {
-               return totalBlocks;
-       }
-
-       /**
-        * Sets the total number of blocks of a request.
-        * 
-        * @param totalBlocks
-        *            The total number of blocks
-        */
-       public void setTotalBlocks(int totalBlocks) {
-               int oldTotalBlocks = this.totalBlocks;
-               this.totalBlocks = totalBlocks;
-               if (oldTotalBlocks != totalBlocks) {
-                       firePropertyChange(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks);
-               }
-       }
-
-       /**
-        * @return the requiredBlocks
-        */
-       public int getRequiredBlocks() {
-               return requiredBlocks;
-       }
-
-       /**
-        * @param requiredBlocks
-        *            the requiredBlocks to set
-        */
-       public void setRequiredBlocks(int requiredBlocks) {
-               int oldRequiredBlocks = this.requiredBlocks;
-               this.requiredBlocks = requiredBlocks;
-               if (oldRequiredBlocks != requiredBlocks) {
-                       firePropertyChange(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks);
-               }
-       }
-
-       /**
-        * @return the successfulBlocks
-        */
-       public int getSuccessfulBlocks() {
-               return successfulBlocks;
-       }
-
-       /**
-        * @param successfulBlocks
-        *            the successfulBlocks to set
-        */
-       public void setSuccessfulBlocks(int successfulBlocks) {
-               int oldSuccessfulBlocks = this.successfulBlocks;
-               this.successfulBlocks = successfulBlocks;
-               if (oldSuccessfulBlocks != successfulBlocks) {
-                       firePropertyChange(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks);
-               }
-       }
-
-       /**
-        * @return the failedBlocks
-        */
-       public int getFailedBlocks() {
-               return failedBlocks;
-       }
-
-       /**
-        * @param failedBlocks
-        *            the failedBlocks to set
-        */
-       public void setFailedBlocks(int failedBlocks) {
-               int oldFailedBlocks = this.failedBlocks;
-               this.failedBlocks = failedBlocks;
-               if (oldFailedBlocks != failedBlocks) {
-                       firePropertyChange(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks);
-               }
-       }
-
-       /**
-        * @return the fatallyFailedBlocks
-        */
-       public int getFatallyFailedBlocks() {
-               return fatallyFailedBlocks;
-       }
-
-       /**
-        * @param fatallyFailedBlocks
-        *            the fatallyFailedBlocks to set
-        */
-       public void setFatallyFailedBlocks(int fatallyFailedBlocks) {
-               int oldFatallyFailedBlocks = this.fatallyFailedBlocks;
-               this.fatallyFailedBlocks = fatallyFailedBlocks;
-               if (oldFatallyFailedBlocks != fatallyFailedBlocks) {
-                       firePropertyChange(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks);
-               }
-       }
-
-       /**
-        * @return the totalFinalized
-        */
-       public boolean isTotalFinalized() {
-               return totalFinalized;
-       }
-
-       /**
-        * @param totalFinalized
-        *            the totalFinalized to set
-        */
-       public void setTotalFinalized(boolean totalFinalized) {
-               boolean oldTotalFinalized = this.totalFinalized;
-               this.totalFinalized = totalFinalized;
-               if (oldTotalFinalized != totalFinalized) {
-                       firePropertyChange(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized);
-               }
        }
 
 }