Oracle® Fusion Middleware C++ API Reference for Oracle Coherence
12c (12.2.1.3.0)

E80355-01

coherence/util/Optional.hpp

00001 /*
00002 * Optional.hpp
00003 *
00004 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_OPTIONAL_HPP
00017 #define COH_OPTIONAL_HPP
00018 
00019 #include "coherence/lang.ns"
00020 
00021 #include "coherence/io/pof/PortableObject.hpp"
00022 #include "coherence/io/pof/PofReader.hpp"
00023 #include "coherence/io/pof/PofWriter.hpp"
00024 
00025 COH_OPEN_NAMESPACE2(coherence,util)
00026 
00027 using coherence::io::pof::PofReader;
00028 using coherence::io::pof::PofWriter;
00029 using coherence::io::pof::PortableObject;
00030 
00031 /**
00032  * C++ port of java.util.Optional, a container object which may or may not
00033  * contain a non-NULL value.  If a value is present, isPresent() will return
00034  * true and get() will return the value.
00035  *
00036  * @author as,lh  2015.06.09
00037  * @since Coherence 12.2.1
00038  */
00039 class COH_EXPORT Optional
00040     : public class_spec<Optional,
00041         extends<Object>,
00042         implements<PortableObject> >
00043     {
00044     friend class factory<Optional>;
00045 
00046     // ----- constructors ---------------------------------------------------
00047 
00048     protected:
00049         /**
00050          * Default constructor.
00051          */
00052         Optional();
00053 
00054     private:
00055         /**
00056          * Constructs an instance with the value present.
00057          *
00058          * @param voValue  the object value it wraps
00059          */
00060         Optional(Object::Holder oh);
00061 
00062         /**
00063          * Blocked copy constructor.
00064          */
00065         Optional(const Optional&);
00066 
00067     public:
00068         /**
00069          * Return an Optional instance with NULL object value.
00070          *
00071          * @return an Optional instance with NULL object value
00072          */
00073         static Optional::View empty();
00074 
00075         /**
00076          * Return an Optional instance with the given object value.
00077          *
00078          * @param voValue  the object value it wraps
00079          *
00080          * @return an Optional instance with the given object value
00081          */
00082         static Optional::View of(Object::Holder oh);
00083 
00084         /**
00085          * Return an Optional instance with the given value, if non-NULL.
00086          * Otherwise, return an empty Optional.
00087          *
00088          * @param oh  the object value it wraps
00089          *
00090          * @return an Optional instance with the given nullable value.
00091          */
00092         static Optional::View ofNullable(Object::Holder oh);
00093 
00094         /**
00095          * Return the object value.
00096          *
00097          * @return the object value
00098          */
00099         virtual Object::Holder get() const;
00100 
00101         /**
00102          * Return true if there is a value present, otherwise false.
00103          *
00104          * @return true if there is a value present, otherwise, false
00105          */
00106         virtual bool isPresent() const;
00107 
00108         /**
00109          * Return value if present, otherwise, the given object.
00110          *
00111          * @param oh the default object value to return
00112          *
00113          * @return the value if present, otherwise, return the given object
00114          */
00115         virtual Object::Holder orElse(Object::Holder oh) const;
00116 
00117     // ----- PortableObject interface ---------------------------------------
00118 
00119     public:
00120         /**
00121          * {@inheritDoc}
00122          */
00123         virtual void readExternal(PofReader::Handle hIn);
00124 
00125         /**
00126          * {@inheritDoc}
00127          */
00128         virtual void writeExternal(PofWriter::Handle hOut) const;
00129 
00130     // ----- Object interface -----------------------------------------------
00131 
00132     public:
00133         /**
00134          * {@inheritDoc}
00135          */
00136         virtual bool equals(Object::View v) const;
00137 
00138         /**
00139          * {@inheritDoc}
00140          */
00141         virtual size32_t hashCode() const;
00142 
00143         /**
00144          * {@inheritDoc}
00145          */
00146         virtual TypedHandle<const String> toString() const;
00147 
00148     // ----- constants ------------------------------------------------------
00149 
00150     public:
00151         /**
00152          * Return an empty Optional instance.
00153          *
00154          * @return the empty Optional
00155          */
00156         static Optional::View emptyInstance();
00157 
00158         // ----- data members ---------------------------------------------------
00159 
00160     private:
00161         /**
00162          * The value this class wraps.  May be NULL.
00163          */
00164         FinalHolder<Object> f_oh;
00165     };
00166 
00167 COH_CLOSE_NAMESPACE2
00168 
00169 #endif // COH_OPTIONAL_HPP
Copyright © 2000, 2017, Oracle and/or its affiliates. All rights reserved.