Reformat source code, new line length for comments (79), some trailing whitespace...
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / Verbosity.java
1 /*
2  * jFCPlib - Verbosity.java - Copyright © 2008 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package net.pterodactylus.fcp;
20
21 /**
22  * Convenicence class for verbosity handling. This might come in handy with the
23  * {@link ClientPut} and {@link ClientGet} requests. The verbosity is a
24  * bit-mask that can be composed of several bits. {@link #PROGRESS} and
25  * {@link #COMPRESSION} are single bits in that mask and can be combined into a
26  * new verbosity using {@link #add(Verbosity)}.
27  *
28  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
29  */
30 public class Verbosity {
31
32         /** Constant for no verbosity at all. */
33         public static final Verbosity NONE = new Verbosity(0);
34
35         /** Constant for progress message verbosity. */
36         public static final Verbosity PROGRESS = new Verbosity(1);
37
38         /** Constant for compression message verbosity. */
39         public static final Verbosity COMPRESSION = new Verbosity(512);
40
41         /** Constant for all events. */
42         public static final Verbosity ALL = new Verbosity(-1);
43
44         /** The verbosity level. */
45         private final int level;
46
47         /**
48          * Creates a new verbosity with the given level.
49          *
50          * @param level
51          *            The verbosity level
52          */
53         private Verbosity(int level) {
54                 this.level = level;
55         }
56
57         /**
58          * Adds the given verbosity to this verbosity and returns a verbosity with
59          * the new value. The value of this verbosity is not changed.
60          *
61          * @param verbosity
62          *            The verbosity to add to this verbosity
63          * @return The verbosity with the new level.
64          */
65         public Verbosity add(Verbosity verbosity) {
66                 return new Verbosity(level | verbosity.level);
67         }
68
69         /**
70          * Checks whether this Verbosity contains all bits of the given Verbosity.
71          *
72          * @param verbosity
73          *            The verbosity to check for in this Verbosity
74          * @return <code>true</code> if and only if all set bits in the given
75          *         Verbosity are also set in this Verbosity
76          */
77         public boolean contains(Verbosity verbosity) {
78                 return (level & verbosity.level) == verbosity.level;
79         }
80
81         /**
82          * @see java.lang.Object#toString()
83          */
84         @Override
85         public String toString() {
86                 return String.valueOf(level);
87         }
88
89         /**
90          * Parses the given string and creates a Verbosity with the given level.
91          *
92          * @param s
93          *            The string to parse
94          * @return The parsed verbosity, or {@link #NONE} if the string could not
95          *         be parsed
96          */
97         public static Verbosity valueOf(String s) {
98                 try {
99                         return new Verbosity(Integer.valueOf(s));
100                 } catch (NumberFormatException nfe1) {
101                         return NONE;
102                 }
103         }
104
105 }