-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBit.java
More file actions
63 lines (53 loc) · 1.68 KB
/
Bit.java
File metadata and controls
63 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Copyright 2019-Present DataCompressionPrimitives.
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*/
package org.dcp.entities.bit;
public class Bit {
public static final Bit FALSE = new Bit(false);
public static final Bit TRUE = new Bit(true);
private final boolean bitValue;
private Bit(final boolean bitValue) {
this.bitValue = bitValue;
}
public static Bit valueOf(final boolean boolValue) {
if (boolValue == false) return FALSE;
else return TRUE;
}
public static Bit valueOf(final String stringValue) {
if ("0".equals(stringValue)) return FALSE;
else if ("1".equals(stringValue)) return TRUE;
else
throw new IllegalArgumentException(
String.format("Value should be 0 or 1. Value: %s", stringValue));
}
public static Bit valueOf(final char charValue) {
if (charValue == '0') return FALSE;
else if (charValue == '1') return TRUE;
else
throw new IllegalArgumentException(
String.format("Value should be 0 or 1. Value: %c", charValue));
}
public static Bit valueOf(final long intValue) {
if (intValue == 0) return FALSE;
else if (intValue == 1) return TRUE;
else
throw new IllegalArgumentException(
String.format("Value should be 0 or 1. Value: %d", intValue));
}
public boolean value() {
return bitValue;
}
public long intValue() {
if (bitValue == false) return 0;
else return 1;
}
public String toString() {
if (bitValue == false) return "0";
else return "1";
}
}