|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 5 | + * https://oss.oracle.com/licenses/upl. |
| 6 | + */ |
| 7 | +using System; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.IO; |
| 10 | +using System.Text; |
| 11 | + |
| 12 | +using Tangosol.IO.Pof; |
| 13 | + |
| 14 | +namespace Tangosol.Util.Extractor |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Universal <see cref="IValueExtractor"/> implementation. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// UniversalExtractor can only run within the Coherence cluster. |
| 21 | + /// Refer to the Coherence for Java documentation for more information. |
| 22 | + /// </remarks> |
| 23 | + /// <author>Cameron Purdy 2002.11.01</author> |
| 24 | + /// <author>Gene Gleyzer 2002.11.01</author> |
| 25 | + /// <author>Everett Williams 2007.02.01</author> |
| 26 | + /// <author>Joe Fialli 2017.11.20</author> |
| 27 | + /// <author>Patrick Fry 2024.09.13</author> |
| 28 | + /// <since>14.1.2.0.0</since> |
| 29 | + public class UniversalExtractor : AbstractExtractor, IValueExtractor, IPortableObject |
| 30 | + { |
| 31 | + #region Properties |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Get the method or property name. |
| 35 | + /// </summary> |
| 36 | + /// <value> |
| 37 | + /// the method or property name. |
| 38 | + /// </value> |
| 39 | + public virtual string Name |
| 40 | + { |
| 41 | + get { return m_name; } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the array of arguments used to invoke the method. |
| 46 | + /// </summary> |
| 47 | + /// <value> |
| 48 | + /// The array of arguments used to invoke the method. |
| 49 | + /// </value> |
| 50 | + public virtual Object[] Parameters |
| 51 | + { |
| 52 | + get { return m_parameters; } |
| 53 | + } |
| 54 | + |
| 55 | + #endregion |
| 56 | + |
| 57 | + #region Constructors |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Default constructor (necessary for the IPortableObject interface). |
| 61 | + /// </summary> |
| 62 | + public UniversalExtractor() |
| 63 | + {} |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Construct a <b>UniversalExtractor</b> based on a member name. |
| 67 | + /// </summary> |
| 68 | + /// <param name="name"> |
| 69 | + /// A method or property name. |
| 70 | + /// </param> |
| 71 | + public UniversalExtractor(string name) |
| 72 | + : this(name, null, VALUE) |
| 73 | + { |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Construct a <b>UniversalExtractor</b>. |
| 78 | + /// </summary> |
| 79 | + /// <param name="name"> |
| 80 | + /// A method or property name. |
| 81 | + /// </param> |
| 82 | + /// <param name="parameters"> |
| 83 | + /// The array of arguments to be used in the method invocation; |
| 84 | + /// may be <c>null</c>. |
| 85 | + /// </param> |
| 86 | + public UniversalExtractor(string name, object[] parameters) |
| 87 | + : this(name, parameters, VALUE) |
| 88 | + { |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Construct a <b>UniversalExtractor</b> based on a method name, |
| 93 | + /// optional parameters and the entry extraction target. |
| 94 | + /// </summary> |
| 95 | + /// <param name="name"> |
| 96 | + /// A method or property name. |
| 97 | + /// </param> |
| 98 | + /// <param name="parameters"> |
| 99 | + /// The array of arguments to be used in the method invocation; |
| 100 | + /// may be <c>null</c>. |
| 101 | + /// </param> |
| 102 | + /// <param name="target"> |
| 103 | + /// One of the <see cref="AbstractExtractor.VALUE"/> or |
| 104 | + /// <see cref="AbstractExtractor.KEY"/> values |
| 105 | + /// </param> |
| 106 | + public UniversalExtractor(string name, object[] parameters, int target) |
| 107 | + { |
| 108 | + Debug.Assert(name != null); |
| 109 | + |
| 110 | + if (parameters != null && parameters.Length > 0 && !name.EndsWith(METHOD_SUFFIX)) |
| 111 | + { |
| 112 | + throw new ArgumentException("UniversalExtractor constructor: parameter name[value:" + name + "] must end with method suffix \"" + METHOD_SUFFIX + "\" when optional parameters provided"); |
| 113 | + } |
| 114 | + m_name = name; |
| 115 | + m_parameters = parameters; |
| 116 | + m_target = target; |
| 117 | + } |
| 118 | + |
| 119 | + #endregion |
| 120 | + |
| 121 | + #region Object override methods |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Provide a human-readable description of this |
| 125 | + /// <see cref="IValueExtractor"/> object. |
| 126 | + /// </summary> |
| 127 | + /// <returns> |
| 128 | + /// A human-readable description of this <b>IValueExtractor</b> |
| 129 | + /// object. |
| 130 | + /// </returns> |
| 131 | + public override string ToString() |
| 132 | + { |
| 133 | + Object[] parameters = m_parameters; |
| 134 | + int cParams = parameters == null ? 0 : parameters.Length; |
| 135 | + |
| 136 | + StringBuilder sb = new StringBuilder(); |
| 137 | + if (m_target == KEY) |
| 138 | + { |
| 139 | + sb.Append(".Key"); |
| 140 | + } |
| 141 | + sb.Append('.').Append(m_name).Append('('); |
| 142 | + for (int i = 0; i < cParams; i++) |
| 143 | + { |
| 144 | + if (i != 0) |
| 145 | + { |
| 146 | + sb.Append(", "); |
| 147 | + } |
| 148 | + sb.Append(parameters[i]); |
| 149 | + } |
| 150 | + sb.Append(')'); |
| 151 | + |
| 152 | + return sb.ToString(); |
| 153 | + } |
| 154 | + |
| 155 | + #endregion |
| 156 | + |
| 157 | + #region IPortableObject implementation |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Restore the contents of a user type instance by reading its state |
| 161 | + /// using the specified <see cref="IPofReader"/> object. |
| 162 | + /// </summary> |
| 163 | + /// <param name="reader"> |
| 164 | + /// The <b>IPofReader</b> from which to read the object's state. |
| 165 | + /// </param> |
| 166 | + /// <exception cref="IOException"> |
| 167 | + /// If an I/O error occurs. |
| 168 | + /// </exception> |
| 169 | + public virtual void ReadExternal(IPofReader reader) |
| 170 | + { |
| 171 | + m_name = reader.ReadString(0); |
| 172 | + m_parameters = (object[]) reader.ReadArray(1); |
| 173 | + m_target = reader.ReadInt32(2); |
| 174 | + } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Save the contents of a POF user type instance by writing its |
| 178 | + /// state using the specified <see cref="IPofWriter"/> object. |
| 179 | + /// </summary> |
| 180 | + /// <param name="writer"> |
| 181 | + /// The <b>IPofWriter</b> to which to write the object's state. |
| 182 | + /// </param> |
| 183 | + /// <exception cref="IOException"> |
| 184 | + /// If an I/O error occurs. |
| 185 | + /// </exception> |
| 186 | + public virtual void WriteExternal(IPofWriter writer) |
| 187 | + { |
| 188 | + string name = m_name; |
| 189 | + if (name == null) |
| 190 | + { |
| 191 | + throw new InvalidOperationException("UniversalExtractor was constructed without a method name"); |
| 192 | + } |
| 193 | + writer.WriteString(0, name); |
| 194 | + writer.WriteArray(1, m_parameters); |
| 195 | + writer.WriteInt32(2, m_target); |
| 196 | + } |
| 197 | + |
| 198 | + #endregion |
| 199 | + |
| 200 | + #region Data members |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// The name of the member to invoke. |
| 204 | + /// </summary> |
| 205 | + protected string m_name; |
| 206 | + |
| 207 | + /// <summary> |
| 208 | + /// The parameter array. |
| 209 | + /// </summary> |
| 210 | + protected Object[] m_parameters; |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// If m_name ends with this suffix, it represents a method name. |
| 214 | + /// </summary> |
| 215 | + public static readonly string METHOD_SUFFIX = "()"; |
| 216 | + |
| 217 | + #endregion |
| 218 | + } |
| 219 | +} |
0 commit comments