From 68380f0f6681c72a5d4ab5e69abc9670e9d99838 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 30 Jul 2015 13:03:45 +0100 Subject: Rename ThrowHelper to Preconditions and make it public - we'll want to use it from the generated code soon. Additionally, change it to return the value passed, and make it generic with a class constraint. A separate method doesn't have the class constraint, for more unusual scenarios. --- csharp/src/Google.Protobuf/Collections/MapField.cs | 16 ++--- csharp/src/Google.Protobuf/Google.Protobuf.csproj | 2 +- csharp/src/Google.Protobuf/JsonFormatter.cs | 2 +- csharp/src/Google.Protobuf/MessageExtensions.cs | 28 ++++---- csharp/src/Google.Protobuf/MessageParser.cs | 4 +- csharp/src/Google.Protobuf/Preconditions.cs | 74 ++++++++++++++++++++++ csharp/src/Google.Protobuf/ThrowHelper.cs | 53 ---------------- 7 files changed, 100 insertions(+), 79 deletions(-) create mode 100644 csharp/src/Google.Protobuf/Preconditions.cs delete mode 100644 csharp/src/Google.Protobuf/ThrowHelper.cs (limited to 'csharp/src') diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index 9f460e1e..fc94fd5c 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -112,13 +112,13 @@ namespace Google.Protobuf.Collections public bool ContainsKey(TKey key) { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); return map.ContainsKey(key); } public bool Remove(TKey key) { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); LinkedListNode> node; if (map.TryGetValue(key, out node)) { @@ -151,7 +151,7 @@ namespace Google.Protobuf.Collections { get { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); TValue value; if (TryGetValue(key, out value)) { @@ -161,11 +161,11 @@ namespace Google.Protobuf.Collections } set { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNullUnconstrained(key, "key"); // value == null check here is redundant, but avoids boxing. if (value == null && !allowNullValues) { - ThrowHelper.ThrowIfNull(value, "value"); + Preconditions.CheckNotNullUnconstrained(value, "value"); } LinkedListNode> node; var pair = new KeyValuePair(key, value); @@ -187,7 +187,7 @@ namespace Google.Protobuf.Collections public void Add(IDictionary entries) { - ThrowHelper.ThrowIfNull(entries, "entries"); + Preconditions.CheckNotNull(entries, "entries"); foreach (var pair in entries) { Add(pair.Key, pair.Value); @@ -374,7 +374,7 @@ namespace Google.Protobuf.Collections void IDictionary.Remove(object key) { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNull(key, "key"); if (!(key is TKey)) { return; @@ -403,7 +403,7 @@ namespace Google.Protobuf.Collections { get { - ThrowHelper.ThrowIfNull(key, "key"); + Preconditions.CheckNotNull(key, "key"); if (!(key is TKey)) { return null; diff --git a/csharp/src/Google.Protobuf/Google.Protobuf.csproj b/csharp/src/Google.Protobuf/Google.Protobuf.csproj index b141e434..7de4f51b 100644 --- a/csharp/src/Google.Protobuf/Google.Protobuf.csproj +++ b/csharp/src/Google.Protobuf/Google.Protobuf.csproj @@ -96,7 +96,7 @@ - + diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index f624b090..438af4df 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -120,7 +120,7 @@ namespace Google.Protobuf public string Format(IMessage message) { - ThrowHelper.ThrowIfNull(message, "message"); + Preconditions.CheckNotNull(message, "message"); StringBuilder builder = new StringBuilder(); // TODO(jonskeet): Handle well-known types here. // Our reflection support needs improving so that we can get at the descriptor diff --git a/csharp/src/Google.Protobuf/MessageExtensions.cs b/csharp/src/Google.Protobuf/MessageExtensions.cs index ee2863d1..7bd79930 100644 --- a/csharp/src/Google.Protobuf/MessageExtensions.cs +++ b/csharp/src/Google.Protobuf/MessageExtensions.cs @@ -41,8 +41,8 @@ namespace Google.Protobuf { public static void MergeFrom(this IMessage message, byte[] data) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(data, "data"); CodedInputStream input = CodedInputStream.CreateInstance(data); message.MergeFrom(input); input.CheckLastTagWas(0); @@ -50,8 +50,8 @@ namespace Google.Protobuf public static void MergeFrom(this IMessage message, ByteString data) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(data, "data"); CodedInputStream input = data.CreateCodedInput(); message.MergeFrom(input); input.CheckLastTagWas(0); @@ -59,8 +59,8 @@ namespace Google.Protobuf public static void MergeFrom(this IMessage message, Stream input) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(input, "input"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(input, "input"); CodedInputStream codedInput = CodedInputStream.CreateInstance(input); message.MergeFrom(codedInput); codedInput.CheckLastTagWas(0); @@ -68,8 +68,8 @@ namespace Google.Protobuf public static void MergeDelimitedFrom(this IMessage message, Stream input) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(input, "input"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(input, "input"); int size = (int) CodedInputStream.ReadRawVarint32(input); Stream limitedStream = new LimitedInputStream(input, size); message.MergeFrom(limitedStream); @@ -77,7 +77,7 @@ namespace Google.Protobuf public static byte[] ToByteArray(this IMessage message) { - ThrowHelper.ThrowIfNull(message, "message"); + Preconditions.CheckNotNull(message, "message"); byte[] result = new byte[message.CalculateSize()]; CodedOutputStream output = CodedOutputStream.CreateInstance(result); message.WriteTo(output); @@ -87,8 +87,8 @@ namespace Google.Protobuf public static void WriteTo(this IMessage message, Stream output) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(output, "output"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(output, "output"); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); message.WriteTo(codedOutput); codedOutput.Flush(); @@ -96,8 +96,8 @@ namespace Google.Protobuf public static void WriteDelimitedTo(this IMessage message, Stream output) { - ThrowHelper.ThrowIfNull(message, "message"); - ThrowHelper.ThrowIfNull(output, "output"); + Preconditions.CheckNotNull(message, "message"); + Preconditions.CheckNotNull(output, "output"); CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); codedOutput.WriteRawVarint32((uint)message.CalculateSize()); message.WriteTo(codedOutput); @@ -106,7 +106,7 @@ namespace Google.Protobuf public static ByteString ToByteString(this IMessage message) { - ThrowHelper.ThrowIfNull(message, "message"); + Preconditions.CheckNotNull(message, "message"); return ByteString.AttachBytes(message.ToByteArray()); } } diff --git a/csharp/src/Google.Protobuf/MessageParser.cs b/csharp/src/Google.Protobuf/MessageParser.cs index 5407de06..bfa63ae5 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -84,7 +84,7 @@ namespace Google.Protobuf /// The newly parsed message. public T ParseFrom(byte[] data) { - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; @@ -92,7 +92,7 @@ namespace Google.Protobuf public T ParseFrom(ByteString data) { - ThrowHelper.ThrowIfNull(data, "data"); + Preconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; diff --git a/csharp/src/Google.Protobuf/Preconditions.cs b/csharp/src/Google.Protobuf/Preconditions.cs new file mode 100644 index 00000000..ff3bd0f1 --- /dev/null +++ b/csharp/src/Google.Protobuf/Preconditions.cs @@ -0,0 +1,74 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +using System; + +namespace Google.Protobuf +{ + /// + /// Helper methods for throwing exceptions + /// + public static class Preconditions + { + /// + /// Throws an ArgumentNullException if the given value is null, otherwise + /// return the value to the caller. + /// + public static T CheckNotNull(T value, string name) where T : class + { + if (value == null) + { + throw new ArgumentNullException(name); + } + return value; + } + + /// + /// Throws an ArgumentNullException if the given value is null, otherwise + /// return the value to the caller. + /// + /// + /// This is equivalent to but without the type parameter + /// constraint. In most cases, the constraint is useful to prevent you from calling CheckNotNull + /// with a value type - but it gets in the way if either you want to use it with a nullable + /// value type, or you want to use it with an unconstrained type parameter. + /// + internal static T CheckNotNullUnconstrained(T value, string name) + { + if (value == null) + { + throw new ArgumentNullException(name); + } + return value; + } + } +} \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/ThrowHelper.cs b/csharp/src/Google.Protobuf/ThrowHelper.cs deleted file mode 100644 index 62d9ea60..00000000 --- a/csharp/src/Google.Protobuf/ThrowHelper.cs +++ /dev/null @@ -1,53 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; - -namespace Google.Protobuf -{ - /// - /// Helper methods for throwing exceptions - /// - internal static class ThrowHelper - { - /// - /// Throws an ArgumentNullException if the given value is null. - /// - internal static void ThrowIfNull(object value, string name) - { - if (value == null) - { - throw new ArgumentNullException(name); - } - } - } -} \ No newline at end of file -- cgit v1.2.3