Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / Verbosity.java
1 /*
2  * jFCPlib - Verbosity.java - Copyright © 2008–2016 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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.fcp;
19
20 /**
21  * Convenicence class for verbosity handling. This might come in handy with the
22  * {@link ClientPut} and {@link ClientGet} requests. The verbosity is a
23  * bit-mask that can be composed of several bits. {@link #PROGRESS} and
24  * {@link #COMPRESSION} are single bits in that mask and can be combined into a
25  * new verbosity using {@link #add(Verbosity)}.
26  *
27  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
28  */
29 public class Verbosity {
30
31         /** Constant for no verbosity at all. */
32         public static final Verbosity NONE = new Verbosity(0);
33
34         /** Constant for progress message verbosity. */
35         public static final Verbosity PROGRESS = new Verbosity(1);
36
37         /** Constant for compression message verbosity. */
38         public static final Verbosity COMPRESSION = new Verbosity(512);
39
40         /** Constant for all events. */
41         public static final Verbosity ALL = new Verbosity(-1);
42
43         /** The verbosity level. */
44         private final int level;
45
46         /**
47          * Creates a new verbosity with the given level.
48          *
49          * @param level
50          *            The verbosity level
51          */
52         private Verbosity(int level) {
53                 this.level = level;
54         }
55
56         /**
57          * Adds the given verbosity to this verbosity and returns a verbosity with
58          * the new value. The value of this verbosity is not changed.
59          *
60          * @param verbosity
61          *            The verbosity to add to this verbosity
62          * @return The verbosity with the new level.
63          */
64         public Verbosity add(Verbosity verbosity) {
65                 return new Verbosity(level | verbosity.level);
66         }
67
68         /**
69          * Checks whether this Verbosity contains all bits of the given Verbosity.
70          *
71          * @param verbosity
72          *            The verbosity to check for in this Verbosity
73          * @return <code>true</code> if and only if all set bits in the given
74          *         Verbosity are also set in this Verbosity
75          */
76         public boolean contains(Verbosity verbosity) {
77                 return (level & verbosity.level) == verbosity.level;
78         }
79
80         /**
81          * @see java.lang.Object#toString()
82          */
83         @Override
84         public String toString() {
85                 return String.valueOf(level);
86         }
87
88         /**
89          * Parses the given string and creates a Verbosity with the given level.
90          *
91          * @param s
92          *            The string to parse
93          * @return The parsed verbosity, or {@link #NONE} if the string could not
94          *         be parsed
95          */
96         public static Verbosity valueOf(String s) {
97                 try {
98                         return new Verbosity(Integer.valueOf(s));
99                 } catch (NumberFormatException nfe1) {
100                         return NONE;
101                 }
102         }
103
104 }