d536914985cb7756c92cc1a9b55c2ffb2131301b
[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 David ‘Bombe’ Roden <bombe@freenetproject.org>
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         /** Constant for all events. */
29         public static final Verbosity ALL = new Verbosity(-1);
30
31         /** The verbosity level. */
32         private final int level;
33
34         /**
35          * Creates a new verbosity with the given level.
36          * 
37          * @param level
38          *            The verbosity level
39          */
40         private Verbosity(int level) {
41                 this.level = level;
42         }
43
44         /**
45          * Adds the given verbosity to this verbosity and returns a verbosity with
46          * the new value. The value of this verbosity is not changed.
47          * 
48          * @param verbosity
49          *            The verbosity to add to this verbosity
50          * @return The verbosity with the new level.
51          */
52         public Verbosity add(Verbosity verbosity) {
53                 return new Verbosity(level | verbosity.level);
54         }
55
56         /**
57          * Checks whether this Verbosity contains all bits of the given Verbosity.
58          * 
59          * @param verbosity
60          *            The verbosity to check for in this Verbosity
61          * @return <code>true</code> if and only if all set bits in the given
62          *         Verbosity are also set in this Verbosity
63          */
64         public boolean contains(Verbosity verbosity) {
65                 return (level & verbosity.level) == verbosity.level;
66         }
67
68         /**
69          * @see java.lang.Object#toString()
70          */
71         @Override
72         public String toString() {
73                 return String.valueOf(level);
74         }
75
76         /**
77          * Parses the given string and creates a Verbosity with the given level.
78          * 
79          * @param s
80          *            The string to parse
81          * @return The parsed verbosity, or {@link #NONE} if the string could not be
82          *         parsed
83          */
84         public static Verbosity valueOf(String s) {
85                 try {
86                         return new Verbosity(Integer.valueOf(s));
87                 } catch (NumberFormatException nfe1) {
88                         return NONE;
89                 }
90         }
91
92 }