package org.apollo.net.codec.update;

import org.apollo.fs.FileDescriptor;

/**
 * Represents a single 'on-demand' request.
 * 
 * @author Graham
 */
public final class OnDemandRequest implements Comparable<OnDemandRequest> {

	/**
	 * An enumeration containing the different request priorities.
	 * 
	 * @author Graham
	 */
	public enum Priority {

		/**
		 * High priority - used in-game when data is required immediately but has not yet been received.
		 */
		HIGH(0),

		/**
		 * Low priority - used when a file is not required urgently. The client login screen says
		 * "loading extra files.." when low priority loading is being performed.
		 */
		LOW(2),

		/**
		 * Medium priority - used while loading the 'bare minimum' required to run the game.
		 */
		MEDIUM(1);

		/**
		 * Converts the integer value to a priority.
		 * 
		 * @param v The integer value.
		 * @return The priority.
		 * @throws IllegalArgumentException If the value is outside of the range 1-3 inclusive.
		 */
		public static Priority valueOf(int v) {
			switch (v) {
			case 0:
				return HIGH;
			case 1:
				return MEDIUM;
			case 2:
				return LOW;
			default:
				throw new IllegalArgumentException("Priority out of range.");
			}
		}

		/**
		 * The integer value.
		 */
		private final int intValue;

		/**
		 * Creates a priority.
		 * 
		 * @param intValue The integer value.
		 */
		private Priority(int intValue) {
			this.intValue = intValue;
		}

		/**
		 * Converts the priority to an integer.
		 * 
		 * @return The integer value.
		 */
		public int toInteger() {
			return intValue;
		}

	}

	/**
	 * The file descriptor.
	 */
	private final FileDescriptor fileDescriptor;

	/**
	 * The request priority.
	 */
	private final Priority priority;

	/**
	 * Creates the 'on-demand' request.
	 * 
	 * @param fileDescriptor The file descriptor.
	 * @param priority The priority.
	 */
	public OnDemandRequest(FileDescriptor fileDescriptor, Priority priority) {
		this.fileDescriptor = fileDescriptor;
		this.priority = priority;
	}

	@Override
	public int compareTo(OnDemandRequest o) {
		int thisPriority = priority.toInteger();
		int otherPriority = o.priority.toInteger();

		if (thisPriority < otherPriority) {
			return 1;
		} else if (thisPriority == otherPriority) {
			return 0;
		}
		return -1;
	}

	/**
	 * Gets the file descriptor.
	 * 
	 * @return The file descriptor.
	 */
	public FileDescriptor getFileDescriptor() {
		return fileDescriptor;
	}

	/**
	 * Gets the priority.
	 * 
	 * @return The priority.
	 */
	public Priority getPriority() {
		return priority;
	}

}
@@ -462,24 +462,137 @@
 %09%09HIGH(0),%0A%0A
+%09%09/**%0A%09%09 * Medium priority - used while loading the 'bare minimum' required to run the game.%0A%09%09 */%0A%09%09MEDIUM(1),%0A%0A
 %09%09/**%0A%09%09 * L
@@ -770,121 +770,8 @@
 W(2)
-,%0A%0A%09%09/**%0A%09%09 * Medium priority - used while loading the 'bare minimum' required to run the game.%0A%09%09 */%0A%09%09MEDIUM(1)
 ;%0A%0A%09
@@ -841,16 +841,20 @@
 @param v
+alue
  The int
@@ -1026,16 +1026,20 @@
 Of(int v
+alue
 ) %7B%0A%09%09%09s
@@ -1046,16 +1046,20 @@
 witch (v
+alue
 ) %7B%0A%09%09%09c
@@ -1212,16 +1212,41 @@
 of range
+ - received %22 + value + %22
 .%22);%0A%09%09%09
@@ -1304,28 +1304,25 @@
 e final int 
-intV
+v
 alue;%0A%0A%09%09/**
@@ -1365,20 +1365,17 @@
  @param 
-intV
+v
 alue The
@@ -1419,20 +1419,17 @@
 ity(int 
-intV
+v
 alue) %7B%0A
@@ -1440,23 +1440,17 @@
 his.
-intV
+v
 alue = 
-intV
+v
 alue
@@ -1589,12 +1589,9 @@
 urn 
-intV
+v
 alue
@@ -2085,16 +2085,20 @@
 equest o
+ther
 ) %7B%0A%09%09in
@@ -2159,16 +2159,20 @@
 rity = o
+ther
 .priorit

Add received priority value to IllegalArgumentException message.