831a8cde7ffed941ed3a28e9b26fd7ee5d3eb346
[jSite2.git] / src / net / pterodactylus / util / fcp / Verbosity.java
1 /**
2  * © 2008 INA Service GmbH
3  */
4 package net.pterodactylus.util.fcp;
5
6 /**
7  * Convenicence class for verbosity handling. This might come in handy with the
8  * {@link ClientPut} and {@link ClientGet} requests.
9  * 
10  * The verbosity is a bit-mask that can be composed of several bits.
11  * {@link #PROGRESS} and {@link #COMPRESSION} are single bits in that mask and
12  * can be combined into a new verbosity using {@link #add(Verbosity)}.
13  * 
14  * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
15  * @version $Id$
16  */
17 public class Verbosity {
18
19         /** Constant for no verbosity at all. */
20         public static final Verbosity NONE = new Verbosity(0);
21
22         /** Constant for progress message verbosity. */
23         public static final Verbosity PROGRESS = new Verbosity(1);
24
25         /** Constant for compression message verbosity. */
26         public static final Verbosity COMPRESSION = new Verbosity(512);
27
28         /** The verbosity level. */
29         private final int level;
30
31         /**
32          * Creates a new verbosity with the given level.
33          * 
34          * @param level
35          *            The verbosity level
36          */
37         private Verbosity(int level) {
38                 this.level = level;
39         }
40
41         /**
42          * Adds the given verbosity to this verbosity and returns a verbosity with
43          * the new value. The value of this verbosity is not changed.
44          * 
45          * @param verbosity
46          *            The verbosity to add to this verbosity
47          * @return The verbosity with the new level.
48          */
49         public Verbosity add(Verbosity verbosity) {
50                 return new Verbosity(level | verbosity.level);
51         }
52
53         /**
54          * Checks whether this Verbosity contains all bits of the given Verbosity.
55          * 
56          * @param verbosity
57          *            The verbosity to check for in this Verbosity
58          * @return <code>true</code> if and only if all set bits in the given
59          *         Verbosity are also set in this Verbosity
60          */
61         public boolean contains(Verbosity verbosity) {
62                 return (level & verbosity.level) == verbosity.level;
63         }
64
65         /**
66          * @see java.lang.Object#toString()
67          */
68         @Override
69         public String toString() {
70                 return String.valueOf(level);
71         }
72
73         /**
74          * Parses the given string and creates a Verbosity with the given level.
75          * 
76          * @param s
77          *            The string to parse
78          * @return The parsed verbosity, or {@link #NONE} if the string could not be
79          *         parsed
80          */
81         public static Verbosity valueOf(String s) {
82                 try {
83                         return new Verbosity(Integer.valueOf(s));
84                 } catch (NumberFormatException nfe1) {
85                         return NONE;
86                 }
87         }
88
89 }