diff options
author | Mark Spruiell <mes@zeroc.com> | 2012-06-15 15:00:48 -0700 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2012-06-15 15:00:48 -0700 |
commit | 3ff1990582c91418554b00c22314a67bba04de2a (patch) | |
tree | bfaa6b82c33ed17db496042834200641ed3ce670 /java | |
parent | * Minor C++ fixes (diff) | |
download | ice-3ff1990582c91418554b00c22314a67bba04de2a.tar.bz2 ice-3ff1990582c91418554b00c22314a67bba04de2a.tar.xz ice-3ff1990582c91418554b00c22314a67bba04de2a.zip |
* Grammar fixes
* Implementing optional data members in Java
Diffstat (limited to 'java')
-rw-r--r-- | java/src/Ice/Optional.java | 85 | ||||
-rw-r--r-- | java/src/Ice/OutputStream.java | 12 | ||||
-rw-r--r-- | java/src/Ice/OutputStreamI.java | 8 |
3 files changed, 102 insertions, 3 deletions
diff --git a/java/src/Ice/Optional.java b/java/src/Ice/Optional.java new file mode 100644 index 00000000000..87c4b8cb526 --- /dev/null +++ b/java/src/Ice/Optional.java @@ -0,0 +1,85 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +package Ice; + +/** + * Generic class for optional parameters. + **/ +public class Optional<T> +{ + /** + * The value defaults to unset. + **/ + public Optional() + { + _isSet = false; + } + + /** + * Sets the value to the given argument. + * + * @param v The initial value. + **/ + public Optional(T v) + { + _value = v; + _isSet = true; + } + + /** + * Sets the value to a shallow copy of the given optional. + * + * @param opt The source value. + **/ + public Optional(Optional<T> opt) + { + _value = opt._value; + _isSet = opt._isSet; + } + + /** + * Obtains the current value. + * + * @return The current value. + * @throws IllegalStateException If the value is not set. + **/ + public T get() + { + if(!_isSet) + { + throw new IllegalStateException("no value is set"); + } + return _value; + } + + /** + * Sets the value to the given argument. + * + * @param v The new value. + **/ + public void set(T v) + { + _value = v; + _isSet = true; + } + + /** + * Determines whether the value is set. + * + * @return True if the value is set, false otherwise. + **/ + public boolean isSet() + { + return _isSet; + } + + private T _value; + private boolean _isSet; +} diff --git a/java/src/Ice/OutputStream.java b/java/src/Ice/OutputStream.java index 7d7df18b576..b54fc8a3f92 100644 --- a/java/src/Ice/OutputStream.java +++ b/java/src/Ice/OutputStream.java @@ -264,6 +264,14 @@ public interface OutputStream void writePendingObjects(); /** + * Write the header information for an optional value. + * + * @param tag The numeric tag associated with the value. + * @param type The optional type of the value. + **/ + void writeOptional(int tag, Ice.OptionalType type); + + /** * Indicates that marshaling of a request or reply is finished. * * @return The byte sequence containing the encoded request or reply. @@ -280,10 +288,10 @@ public interface OutputStream /** * Inserts a fixed 32-bit size value into the stream at the given position. * + * @param sz The 32-bit size value. * @param pos The position at which to write the value. - * @param sz The size value. **/ - void rewrite(int pos, int sz); + void rewrite(int sz, int pos); /** * Resets this output stream. This method allows the stream to be reused, to avoid creating diff --git a/java/src/Ice/OutputStreamI.java b/java/src/Ice/OutputStreamI.java index d7b4ffd23d5..60973c2fc69 100644 --- a/java/src/Ice/OutputStreamI.java +++ b/java/src/Ice/OutputStreamI.java @@ -243,6 +243,12 @@ public class OutputStreamI implements OutputStream _os.writePendingObjects(); } + public void + writeOptional(int tag, Ice.OptionalType type) + { + _os.writeOpt(tag, type); + } + public byte[] finished() { @@ -260,7 +266,7 @@ public class OutputStreamI implements OutputStream } public void - rewrite(int pos, int sz) + rewrite(int sz, int pos) { _os.writeInt(sz, pos); } |