You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by mh...@apache.org on 2011/09/05 13:06:26 UTC

[Lucene.Net] svn commit: r1165241 - in /incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net: Analysis/CharStream.cs Lucene.Net.csproj Util/BytesRef.cs Util/CharsRef.cs Util/HashCodeUtil.cs

Author: mherndon
Date: Mon Sep  5 11:06:25 2011
New Revision: 1165241

URL: http://svn.apache.org/viewvc?rev=1165241&view=rev
Log:
refactors bytesref & charsRef to use IEquatable<T> instead of the java counter parts named equality methods.  created a hashcodeutil class for refactoring the common overrides of generating a hash code. Added the abstract CharStream class.

Added:
    incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharStream.cs
    incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/HashCodeUtil.cs
Modified:
    incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.csproj
    incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/BytesRef.cs
    incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/CharsRef.cs

Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharStream.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharStream.cs?rev=1165241&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharStream.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharStream.cs Mon Sep  5 11:06:25 2011
@@ -0,0 +1,66 @@
+// -----------------------------------------------------------------------
+// <copyright company="Apache" file="CharStream.cs">
+//
+//      Licensed to the Apache Software Foundation (ASF) under one or more
+//      contributor license agreements.  See the NOTICE file distributed with
+//      this work for additional information regarding copyright ownership.
+//      The ASF licenses this file to You under the Apache License, Version 2.0
+//      (the "License"); you may not use this file except in compliance with
+//      the License.  You may obtain a copy of the License at
+// 
+//      http://www.apache.org/licenses/LICENSE-2.0
+// 
+//      Unless required by applicable law or agreed to in writing, software
+//      distributed under the License is distributed on an "AS IS" BASIS,
+//      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//      See the License for the specific language governing permissions and
+//      limitations under the License.
+//
+// </copyright>
+// -----------------------------------------------------------------------
+
+using System.IO;
+
+namespace Lucene.Net.Analysis
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Linq;
+    using System.Text;
+
+    /// <summary>
+    /// <see cref="CharStream"/> extends <see cref="TextReader"/> in order
+    /// to enforce an extra method <see cref="CorrectOffset"/>. All tokenizers
+    /// accept a <see cref="CharStream"/> instead of <see cref="TextReader"/> for 
+    /// this reason.  
+    /// </summary>
+    /// <remarks>
+    ///     <para>
+    ///         The <see cref="CharStream"/> enables arbitrary character based 
+    ///         filtering before tokenization. 
+    ///     </para>
+    /// </remarks>
+    /// <seealso cref="CorrectOffset"/>
+    public abstract class CharStream : System.IO.TextReader
+    {
+
+        /// <summary>
+        /// Corrects the offset.
+        /// </summary>
+        /// <remarks>
+        ///     <para>
+        ///        <see cref="CorrectOffset"/> fixes offsets to account for
+        ///         removal or insertion of characters, so that the offsets
+        ///         reported in the tokens match the character offsets of the
+        ///         original Reader.
+        ///     </para>
+        ///     <para>
+        ///         <see cref="CorrectOffset"/> is generally invoked by <c>Tokenizer</c> classes
+        ///         and <c>CharFilter</c> classes.
+        ///     </para>
+        /// </remarks>
+        /// <param name="offset">The offset for the output.</param>
+        /// <returns>The <see cref="Int32"/> offset based on the input.</returns>
+        public abstract int CorrectOffset(int offset);
+    }
+}
\ No newline at end of file

Modified: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.csproj
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.csproj?rev=1165241&r1=1165240&r2=1165241&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.csproj (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.csproj Mon Sep  5 11:06:25 2011
@@ -101,6 +101,7 @@
     <Compile Include="Util\AttributeSourceState.cs" />
     <Compile Include="Util\BytesRef.cs" />
     <Compile Include="Util\CharsRef.cs" />
+    <Compile Include="Util\HashCodeUtil.cs" />
     <Compile Include="Util\IAttribute.cs" />
     <Compile Include="Util\RamUsageEstimator.cs" />
     <Compile Include="Util\UnicodeUtil.cs" />

Modified: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/BytesRef.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/BytesRef.cs?rev=1165241&r1=1165240&r2=1165241&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/BytesRef.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/BytesRef.cs Mon Sep  5 11:06:25 2011
@@ -31,7 +31,14 @@ namespace Lucene.Net.Util
     /// TODO: port
     /// Still missing methods that have to do with CharRef and ArrayUtil.Grow
     /// </summary>
+    /// <remarks>
+    ///     <para>
+    ///         This class might be transformed into a immutable value type depending on its use
+    ///         in the Lucene code base.
+    ///     </para>
+    /// </remarks>
     public sealed class BytesRef : IComparable<BytesRef>,
+        IEquatable<BytesRef>,
         ICloneable<BytesRef>
     {
         /// <summary>
@@ -217,7 +224,7 @@ namespace Lucene.Net.Util
         /// </summary>
         /// <param name="target">The target.</param>
         /// <returns>An instance of <see cref="Boolean"/>.</returns>
-        public bool BytesEqual(BytesRef target)
+        public bool Equals(BytesRef target)
         {
             if (this.Length != target.Length)
                 return false;
@@ -358,7 +365,7 @@ namespace Lucene.Net.Util
             if (bytesRef == null)
                 return false;
 
-            return this.BytesEqual(bytesRef);
+            return this.Equals(bytesRef);
         }
 
         /// <summary>

Modified: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/CharsRef.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/CharsRef.cs?rev=1165241&r1=1165240&r2=1165241&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/CharsRef.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/CharsRef.cs Mon Sep  5 11:06:25 2011
@@ -31,7 +31,13 @@ namespace Lucene.Net.Util
     /// This class represents a slice of a an existing <c>char[]</c>.
     /// The <see cref="Chars"/> property should never be null, default to <see cref="emptyArray"/> if necessary.
     /// </summary>
-    public class CharsRef : IComparable<CharsRef>, ICharSequence, ICloneable<CharsRef>, IEnumerable<char>
+    /// <remarks>
+    ///     <para>
+    ///         This class might be transformed into a immutable value type depending on its use
+    ///         in the Lucene code base.
+    ///     </para>
+    /// </remarks>
+    public class CharsRef : IComparable<CharsRef>, ICharSequence, ICloneable<CharsRef>, IEnumerable<char>, IEquatable<CharsRef>
     {
         private static readonly char[] emptyArray = new char[0];
         private static readonly UTF8Comparer comparer = new UTF8Comparer();

Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/HashCodeUtil.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/HashCodeUtil.cs?rev=1165241&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/HashCodeUtil.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/HashCodeUtil.cs Mon Sep  5 11:06:25 2011
@@ -0,0 +1,59 @@
+// -----------------------------------------------------------------------
+// <copyright file="HashCodeUtil.cs" company="Microsoft">
+// TODO: Update copyright text.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Util
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Linq;
+    using System.Text;
+
+    /// <summary>
+    /// TODO: Update summary.
+    /// </summary>
+    public class HashCodeUtil
+    {
+
+        /// <summary>
+        /// Generates the hash code.
+        /// </summary>
+        /// <param name="append">The append.</param>
+        /// <param name="initial">The initial.</param>
+        /// <param name="prime">The prime.</param>
+        /// <returns>An instance of <see cref="Int32"/>.</returns>
+        public static int GenerateHashCode(int append, int initial = 0, int prime = 31)
+        {
+            int code = initial;
+            code = (code * prime) + append;
+            return code;
+        }
+
+        /// <summary>
+        /// Generates the hash code.
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="slices">The slices.</param>
+        /// <param name="offset">The offset.</param>
+        /// <param name="length">The length.</param>
+        /// <param name="prime">The prime.</param>
+        /// <param name="code">The code.</param>
+        /// <returns>An instance of <see cref="Int32"/>.</returns>
+        public static int GenerateHashCode<T>(T[] slices, int offset = 0, int length = 0, int prime = 31, int code = 0) where T : IConvertible
+        {
+            if (length == 0)
+                length = slices.Length;
+
+            int end = offset + length;
+
+            for (int i = offset; i < end; i++)
+            {
+                code = (code * prime) + slices[i].ToInt32(null);
+            }
+
+            return code;
+        }
+    }
+}