lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mhern...@apache.org
Subject [Lucene.Net] svn commit: r1165383 - in /incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net: Analysis/CharReader.cs Analysis/CharStream.cs Lucene.Net.csproj Util/HashCodeUtil.cs
Date Mon, 05 Sep 2011 18:53:47 GMT
Author: mherndon
Date: Mon Sep  5 18:53:47 2011
New Revision: 1165383

URL: http://svn.apache.org/viewvc?rev=1165383&view=rev
Log:
adding CharStream and CharReader classes

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

Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharReader.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharReader.cs?rev=1165383&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharReader.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharReader.cs Mon
Sep  5 18:53:47 2011
@@ -0,0 +1,91 @@
+// -----------------------------------------------------------------------
+// <copyright file="CharReader.cs" company="Microsoft">
+// TODO: Update copyright text.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Analysis
+{
+    using System;
+    using System.Collections.Generic;
+    using System.IO;
+    using System.Linq;
+    using System.Text;
+ 
+
+    /// <summary>
+    /// <see cref="CharReader"/> is wrapper for <see cref="StreamReader"/> that
+    /// reads in <c>char[]</c> and outputs a <see cref="CharStream"/>.
+    /// </summary>
+    /// <seealso cref="CharStream"/>
+    public sealed class CharReader : CharStream
+    {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="CharReader"/> class.
+        /// </summary>
+        /// <param name="reader">The reader.</param>
+        private CharReader(StreamReader reader)
+            : base(reader)
+        { 
+        }
+
+        /// <summary>
+        /// Casts or creates a new CharReader
+        /// </summary>
+        /// <param name="reader">The reader.</param>
+        /// <returns>
+        /// An instance of <see cref="CharReader"/>.
+        /// </returns>
+        public static CharReader CastOrCreate(StreamReader reader)
+        {
+            if (reader is CharReader)
+                return (CharReader)reader;
+
+            return new CharReader(reader);
+        }
+      
+        /// <summary>
+        /// Corrects the offset.
+        /// </summary>
+        /// <param name="offset">The offset for the output.</param>
+        /// <returns>
+        /// The <see cref="Int32"/> offset based on the input.
+        /// </returns>
+        /// <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>
+        public override int CorrectOffset(int offset)
+        {
+            return offset;
+        }
+
+        /// <summary>
+        /// When it is called by trusted applications, reads a maximum of <paramref name="count"/>
characters from the current stream into <paramref name="buffer"/>, beginning at <paramref
name="index"/>.
+        /// </summary>
+        /// <param name="buffer">When this method returns, contains the specified character
array with the values between <paramref name="index"/> and (<paramref name="index
"/>+ <paramref name="count"/> - 1) replaced by the characters read from the current
source.</param>
+        /// <param name="index">The index of <paramref name="buffer"/> at which
to begin writing.</param>
+        /// <param name="count">The maximum number of characters to read.</param>
+        /// <returns>
+        /// The number of characters that have been read, or 0 if at the end of the stream
and no data was read. The number will be less than or equal to the <paramref name="count"/>
parameter, depending on whether the data is available within the stream.
+        /// </returns>
+        /// <exception cref="T:System.ArgumentException">The buffer length minus <paramref
name="index"/> is less than <paramref name="count"/>. </exception>
+        /// <exception cref="T:System.ArgumentNullException">
+        ///     <paramref name="buffer"/> is null. </exception>
+        /// <exception cref="T:System.ArgumentOutOfRangeException">
+        ///     <paramref name="index"/> or <paramref name="count"/> is negative.
</exception>
+        /// <exception cref="T:System.IO.IOException">An I/O error occurs, such as
the stream is closed. </exception>
+        public override int Read(char[] buffer, int index, int count)
+        {
+            return this.InnerReader.Read(buffer, index, count);
+        }
+    }
+}

Modified: 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=1165383&r1=1165382&r2=1165383&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharStream.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/CharStream.cs Mon
Sep  5 18:53:47 2011
@@ -19,12 +19,13 @@
 // </copyright>
 // -----------------------------------------------------------------------
 
-using System.IO;
+
 
 namespace Lucene.Net.Analysis
 {
     using System;
     using System.Collections.Generic;
+    using System.IO;
     using System.Linq;
     using System.Text;
 
@@ -39,10 +40,38 @@ namespace Lucene.Net.Analysis
     ///         The <see cref="CharStream"/> enables arbitrary character based 
     ///         filtering before tokenization. 
     ///     </para>
+    ///     <note>
+    ///         The following methods, <see cref="Mark"/>, <see cref="MarkSupported"/>,
+    ///         and <see cref="Reset"/> were added to this abstract class as virtual
methods
+    ///         in case they were expected on all <see cref="CharStream"/> instances
+    ///         due to Java's Reader implementation.  
+    ///     </note>
     /// </remarks>
     /// <seealso cref="CorrectOffset"/>
-    public abstract class CharStream : System.IO.TextReader
+    public abstract class CharStream : System.IO.StreamReader
     {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="CharStream"/> class.
+        /// </summary>
+        /// <param name="reader">The reader.</param>
+        protected CharStream(StreamReader reader)
+            : base(reader.BaseStream)
+        {
+            this.InnerReader = reader;
+            this.CurrentPosition = -1;
+        }
+
+        /// <summary>
+        /// Gets or sets the current position.
+        /// </summary>
+        /// <value>The current position.</value>
+        protected long CurrentPosition { get; set; }
+
+        /// <summary>
+        /// Gets or sets the inner reader.
+        /// </summary>
+        /// <value>The inner reader.</value>
+        protected StreamReader InnerReader { get; set; }
 
         /// <summary>
         /// Corrects the offset.
@@ -62,5 +91,44 @@ namespace Lucene.Net.Analysis
         /// <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);
+
+
+        /// <summary>
+        /// Closes this instance.
+        /// </summary>
+        public virtual void Close()
+        {
+            //// TODO: call close when the PLT finally supports it.
+            this.InnerReader.DiscardBufferedData();
+        }
+
+        /// <summary>
+        /// Determines if the stream supports Marking/Seeking ahead.
+        /// </summary>
+        /// <seealso cref="Stream.CanSeek"/>
+        /// <returns>An instance of <see cref="Boolean"/>.</returns>
+        public virtual bool MarkSupported()
+        {
+            return this.InnerReader.BaseStream.CanSeek;
+        }
+
+        /// <summary>
+        /// Marks the seek position in the stream.
+        /// </summary>
+        /// <param name="readAheadLimit">The limit of characters to read ahead.</param>
+        public virtual void Mark(int readAheadLimit)
+        {
+           this.CurrentPosition = this.InnerReader.BaseStream.Position;
+           this.InnerReader.BaseStream.Position = readAheadLimit;
+        }
+
+        /// <summary>
+        /// Resets the stream's position to the original position after
+        /// <see cref="Mark"/> has been called.
+        /// </summary>
+        public virtual void Reset()
+        {
+            this.InnerReader.BaseStream.Position = this.CurrentPosition;
+        }
     }
 }
\ 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=1165383&r1=1165382&r2=1165383&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 18:53:47 2011
@@ -52,6 +52,8 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Analysis\Analyzer.cs" />
+    <Compile Include="Analysis\CharReader.cs" />
+    <Compile Include="Analysis\CharStream.cs" />
     <Compile Include="Analysis\Token.cs" />
     <Compile Include="Analysis\TokenAttributes\CharTermAttribute.cs" />
     <Compile Include="Analysis\TokenAttributes\ICharTermAttribute.cs" />

Modified: 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=1165383&r1=1165382&r2=1165383&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/HashCodeUtil.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/HashCodeUtil.cs Mon Sep
 5 18:53:47 2011
@@ -12,11 +12,10 @@ namespace Lucene.Net.Util
     using System.Text;
 
     /// <summary>
-    /// TODO: Update summary.
+    /// Utility class for generating hash codes using 31 as the default prime. 
     /// </summary>
     public class HashCodeUtil
     {
-
         /// <summary>
         /// Generates the hash code.
         /// </summary>
@@ -34,14 +33,26 @@ namespace Lucene.Net.Util
         /// <summary>
         /// Generates the hash code.
         /// </summary>
-        /// <typeparam name="T"></typeparam>
+        /// <typeparam name="T">The type of the array.</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>
+        /// <remarks>
+        ///     <para>
+        ///     Unfortunately using the <see cref="IConvertible"/> interface to constrain
<typeparamref name="T"/>
+        ///     to types that could be converted to <see cref="int"/> would cause Lucene.Net
to lose 
+        ///     CLS-compliance.  And Microsoft will not create another interface to fix the
issue:
+        ///      http://connect.microsoft.com/VisualStudio/feedback/details/308902/iconvertable-breaks-cls-compliance-for-classes-that-have-generic-constraints-including-iconvertable
+        ///     </para>
+        /// </remarks>
         /// <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
+        /// <exception cref="InvalidCastException">
+        ///     Thrown when items of the array of <typeparamref name="T"/> can not
be
+        ///     converted to Int32. 
+        /// </exception>
+        public static int GenerateHashCode<T>(T[] slices, int offset = 0, int length
= 0, int prime = 31, int code = 0) 
         {
             if (length == 0)
                 length = slices.Length;
@@ -50,7 +61,7 @@ namespace Lucene.Net.Util
 
             for (int i = offset; i < end; i++)
             {
-                code = (code * prime) + slices[i].ToInt32(null);
+                code = (code * prime) + Convert.ToInt32(slices[i]);
             }
 
             return code;



Mime
View raw message