01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.foundation;
22:
23: import java.io.*;
24:
25: /**
26: * yes/no/dontknow data type
27: *
28: * @exclude
29: */
30: public final class TernaryBool implements Serializable {
31:
32: private static final int NO_ID = -1;
33: private static final int YES_ID = 1;
34: private static final int UNSPECIFIED_ID = 0;
35:
36: public static final TernaryBool NO = new TernaryBool(NO_ID);
37: public static final TernaryBool YES = new TernaryBool(YES_ID);
38: public static final TernaryBool UNSPECIFIED = new TernaryBool(
39: UNSPECIFIED_ID);
40:
41: private final int _value;
42:
43: private TernaryBool(int value) {
44: _value = value;
45: }
46:
47: public boolean booleanValue(boolean defaultValue) {
48: switch (_value) {
49: case NO_ID:
50: return false;
51: case YES_ID:
52: return true;
53: default:
54: return defaultValue;
55: }
56: }
57:
58: public boolean unspecified() {
59: return this == UNSPECIFIED;
60: }
61:
62: public boolean definiteYes() {
63: return this == YES;
64: }
65:
66: public boolean definiteNo() {
67: return this == NO;
68: }
69:
70: public static TernaryBool forBoolean(boolean value) {
71: return (value ? YES : NO);
72: }
73:
74: public boolean equals(Object obj) {
75: if (this == obj) {
76: return true;
77: }
78: if (obj == null || getClass() != obj.getClass()) {
79: return false;
80: }
81: TernaryBool tb = (TernaryBool) obj;
82: return _value == tb._value;
83: }
84:
85: public int hashCode() {
86: return _value;
87: }
88:
89: private Object readResolve() {
90: switch (_value) {
91: case NO_ID:
92: return NO;
93: case YES_ID:
94: return YES;
95: default:
96: return UNSPECIFIED;
97: }
98: }
99: }
|