| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Command |
|
| 2.3333333333333335;2,333 |
| 1 | /* | |
| 2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net | |
| 3 | * Copyright (C) 2004-2010 Frederico Caldeira Knabben | |
| 4 | * | |
| 5 | * == BEGIN LICENSE == | |
| 6 | * | |
| 7 | * Licensed under the terms of any of the following licenses at your | |
| 8 | * choice: | |
| 9 | * | |
| 10 | * - GNU General Public License Version 2 or later (the "GPL") | |
| 11 | * http://www.gnu.org/licenses/gpl.html | |
| 12 | * | |
| 13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") | |
| 14 | * http://www.gnu.org/licenses/lgpl.html | |
| 15 | * | |
| 16 | * - Mozilla Public License Version 1.1 or later (the "MPL") | |
| 17 | * http://www.mozilla.org/MPL/MPL-1.1.html | |
| 18 | * | |
| 19 | * == END LICENSE == | |
| 20 | */ | |
| 21 | package net.fckeditor.handlers; | |
| 22 | ||
| 23 | import java.util.HashMap; | |
| 24 | import java.util.Map; | |
| 25 | ||
| 26 | import net.fckeditor.tool.Utils; | |
| 27 | ||
| 28 | /** | |
| 29 | * File Browser <code>GET</code> and <code>POST</code> commands.<br /> | |
| 30 | * The File Browser sends a specific command for each and every request. This | |
| 31 | * class is intended to reflect these in an Enum-like manner. | |
| 32 | * <p> | |
| 33 | * The commands are for <code>GET</code>: | |
| 34 | * <ul> | |
| 35 | * <li>GetFolders</li> | |
| 36 | * <li>GetFoldersAndFiles</li> | |
| 37 | * <li>CreateFolder</li> | |
| 38 | * </ul> | |
| 39 | * and for <code>POST</code>: | |
| 40 | * <ul> | |
| 41 | * <li>FileUpload</li> | |
| 42 | * <li>QuickUpload</li> | |
| 43 | *</ul> | |
| 44 | * | |
| 45 | * @version $Id: Command.java 4785 2009-12-21 20:10:28Z mosipov $ | |
| 46 | */ | |
| 47 | public class Command { | |
| 48 | ||
| 49 | private String name; | |
| 50 | 1 | private static final Map<String, Command> getCommands = new HashMap<String, Command>( |
| 51 | 3); | |
| 52 | 1 | private static final Map<String, Command> postCommands = new HashMap<String, Command>( |
| 53 | 2); | |
| 54 | /** GET command <code>GetFolders</code> */ | |
| 55 | 1 | public static final Command GET_FOLDERS = new Command("GetFolders"); |
| 56 | /** GET command <code>GetFoldersAndFiles</code> */ | |
| 57 | 1 | public static final Command GET_FOLDERS_AND_FILES = new Command( |
| 58 | "GetFoldersAndFiles"); | |
| 59 | /** GET command <code>CreateFolder</code> */ | |
| 60 | 1 | public static final Command CREATE_FOLDER = new Command("CreateFolder"); |
| 61 | /** POST command <code>FileUpload</code> */ | |
| 62 | 1 | public static final Command FILE_UPLOAD = new Command("FileUpload"); |
| 63 | /** POST command <code>QuickUpload</code> */ | |
| 64 | 1 | public static final Command QUICK_UPLOAD = new Command("QuickUpload"); |
| 65 | ||
| 66 | static { | |
| 67 | // initialize the get commands | |
| 68 | 1 | getCommands.put(GET_FOLDERS.getName(), GET_FOLDERS); |
| 69 | 1 | getCommands.put(GET_FOLDERS_AND_FILES.getName(), GET_FOLDERS_AND_FILES); |
| 70 | 1 | getCommands.put(CREATE_FOLDER.getName(), CREATE_FOLDER); |
| 71 | ||
| 72 | // initialize the post commands | |
| 73 | 1 | postCommands.put(FILE_UPLOAD.getName(), FILE_UPLOAD); |
| 74 | 1 | postCommands.put(QUICK_UPLOAD.getName(), QUICK_UPLOAD); |
| 75 | 1 | } |
| 76 | ||
| 77 | /** | |
| 78 | * Constructs a command with the given name. | |
| 79 | * | |
| 80 | * @param name | |
| 81 | * the name of the new command | |
| 82 | */ | |
| 83 | 5 | private Command(final String name) { |
| 84 | 5 | this.name = name; |
| 85 | 5 | } |
| 86 | ||
| 87 | /** | |
| 88 | * Returns the name of this command. | |
| 89 | * | |
| 90 | * @return the name of this command | |
| 91 | */ | |
| 92 | public String getName() { | |
| 93 | 6 | return name; |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Returns the command constant with the specified name. | |
| 98 | * | |
| 99 | * @param name | |
| 100 | * the name of the constant to return | |
| 101 | * @return the command constant with the specified name | |
| 102 | * @throws IllegalArgumentException | |
| 103 | * if this class has no constant with the specified name | |
| 104 | * @throws NullPointerException | |
| 105 | * if <code>name</code> is null or empty | |
| 106 | */ | |
| 107 | public static Command valueOf(final String name) { | |
| 108 | 10 | if (Utils.isEmpty(name)) |
| 109 | 2 | throw new NullPointerException("Name is null or empty"); |
| 110 | ||
| 111 | 8 | Command command = getCommands.get(name); |
| 112 | 8 | if (command == null) |
| 113 | 5 | command = postCommands.get(name); |
| 114 | 8 | if (command == null) |
| 115 | 2 | throw new IllegalArgumentException("No command const " + name); |
| 116 | ||
| 117 | 6 | return command; |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Returns <code>true</code> if name represents a valid <code>GET</code> | |
| 122 | * command constant. | |
| 123 | * | |
| 124 | * @param name | |
| 125 | * the command to check | |
| 126 | * @return <code>true</code> if name represents a valid command, else | |
| 127 | * <code>false</code> | |
| 128 | */ | |
| 129 | public static boolean isValidForGet(final String name) { | |
| 130 | 0 | return getCommands.containsKey(name); |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Returns <code>true</code> if name represents a valid <code>POST</code> | |
| 135 | * command constant. | |
| 136 | * | |
| 137 | * @param name | |
| 138 | * the command to check | |
| 139 | * @return <code>true</code> if name represents a valid command, else | |
| 140 | * <code>false</code> | |
| 141 | */ | |
| 142 | public static boolean isValidForPost(final String name) { | |
| 143 | 0 | return postCommands.containsKey(name); |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Returns the command constant with the specified name. In contrast to | |
| 148 | * {@link #valueOf(String)} it returns a null instead of throwing an | |
| 149 | * exception if a command constant was not found. | |
| 150 | * | |
| 151 | * @param name | |
| 152 | * the name of the constant to return | |
| 153 | * @return the command constant with the specified name, else | |
| 154 | * <code>null</code> | |
| 155 | */ | |
| 156 | public static Command getCommand(final String name) { | |
| 157 | try { | |
| 158 | 3 | return Command.valueOf(name); |
| 159 | 2 | } catch (Exception e) { |
| 160 | 2 | return null; |
| 161 | } | |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * Compares the specified object with this command for equality. The | |
| 166 | * comparison is based on class and name only. | |
| 167 | * | |
| 168 | * @param obj | |
| 169 | * Object to be compared with this command. | |
| 170 | * @return <code>true</code> if the specified object is equal to this | |
| 171 | * command | |
| 172 | */ | |
| 173 | @Override | |
| 174 | public boolean equals(Object obj) { | |
| 175 | 8 | if (this == obj) |
| 176 | 6 | return true; |
| 177 | ||
| 178 | 2 | if (obj == null || this.getClass() != obj.getClass()) |
| 179 | 1 | return false; |
| 180 | ||
| 181 | 1 | final Command command = (Command) obj; |
| 182 | 1 | return name.equals(command.getName()); |
| 183 | } | |
| 184 | ||
| 185 | /** | |
| 186 | * Returns the hash code value for this command. The hash code equals the | |
| 187 | * hash code of the name field. | |
| 188 | * | |
| 189 | * @return the hash code value for this command | |
| 190 | */ | |
| 191 | @Override | |
| 192 | public int hashCode() { | |
| 193 | 2 | return name.hashCode(); |
| 194 | } | |
| 195 | ||
| 196 | /** | |
| 197 | * Returns a string representation of this command. | |
| 198 | * | |
| 199 | * @return a string representation of this command | |
| 200 | * @see #getName() | |
| 201 | */ | |
| 202 | @Override | |
| 203 | public String toString() { | |
| 204 | 1 | return name; |
| 205 | } | |
| 206 | } |