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