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/08/05 21:21:29 UTC
[Lucene.Net] svn commit: r1154359 [23/25] - in
/incubator/lucene.net/branches/Lucene.Net_4e: ./ build/ build/scripts/
packages/ packages/Gallio.3.2.750/ packages/Gallio.3.2.750/licenses/
packages/Gallio.3.2.750/tools/ packages/Gallio.3.2.750/tools/NCover/
packages/...
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,409 @@
+// -----------------------------------------------------------------------
+// <copyright company="Apache" file="CharTermAttribute.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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Analysis.TokenAttributes
+{
+ using System;
+ using System.Text;
+ using Util;
+
+ /// <summary>
+ /// The term text of a Token
+ /// </summary>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>Java File: </b> <a href="https://github.com/apache/lucene-solr/blob/trunk/lucene/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java">
+ /// lucene/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttributeImpl.java
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs">
+ /// src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# Tests: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/test/Lucene.Net.Test/Analysis/TokenAttributes/CharTermAttributeTest.cs">
+ /// test/Lucene.Net.Test/Analysis/TokenAttributes/CharTermAttributeTest.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// <para>
+ /// The java version has extra methods to work with Java's
+ /// <a href="http://download.oracle.com/javase/1,5.0/docs/api/java/lang/CharSequence.html">CharSequence</a>.
+ /// Java's version of <c>string</c>, <c>StringBuilder</c>, <c>StringBuffer</c>, and <c>CharBuffer</c>
+ /// all implement this interface. C# does not have a known equivalent.
+ /// </para>
+ /// </remarks>
+ public class CharTermAttribute : AttributeBase, ICharTermAttribute
+ {
+ private const int MinBufferSize = 10;
+ private int termLength;
+
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="CharTermAttribute"/> class.
+ /// </summary>
+ public CharTermAttribute()
+ {
+ this.Buffer = CreateBuffer(MinBufferSize);
+ }
+
+ /// <summary>
+ /// Gets the internal termBuffer character array which you can then
+ /// directly alter.
+ /// </summary>
+ /// <value>The buffer.</value>
+ /// <remarks>
+ /// <para>
+ /// If the array is too small for the token, use <see cref="SetLength(int)"/>
+ /// to increase the size. After altering the buffer be sure to call
+ /// <see cref="SetLength(int)"/> to record the valid characters that
+ /// were placed into the termBuffer.
+ /// </para>
+ /// </remarks>
+ public char[] Buffer { get; private set; }
+
+ /// <summary>
+ /// Gets or sets the number of valid characters, the length of the term, in
+ /// the termBuffer array.
+ /// </summary>
+ /// <value></value>
+ /// <remarks>
+ /// <para>
+ /// Use this to truncate the termBuffer or to synchronize any external
+ /// manipulation of the termBuffer.
+ /// </para>
+ /// <note>
+ /// To grow the size of the array, use <see cref="ResizeBuffer(int)"/> first.
+ /// </note>
+ /// </remarks>
+ public int Length
+ {
+ get { return this.termLength; }
+ set { this.SetLength(value); }
+ }
+
+ /// <summary>
+ /// Appends the specified <see cref="Char"/> value.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ public ICharTermAttribute Append(char value)
+ {
+ int newLength = this.termLength + 1;
+
+ this.ResizeBuffer(newLength);
+ this.Buffer[newLength] = value;
+
+ return this;
+ }
+
+ /// <summary>
+ /// Appends the specified <see cref="string"/> to internal buffer or character sequence.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ public ICharTermAttribute Append(string value)
+ {
+ return Append(value, 0, value == null ? 0 : value.Length);
+ }
+
+ /// <summary>
+ /// Appends the specified <see cref="string"/> to internal buffer or character sequence.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <param name="startingIndex">the index of string to start the copy.</param>
+ /// <param name="length">The length of the string that is to be copied.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ public ICharTermAttribute Append(string value, int startingIndex, int length)
+ {
+ if (value == null)
+ return this.AppendNull();
+
+ value.CopyTo(startingIndex, this.ResizeBuffer(this.termLength + length), this.termLength, length);
+ this.Length += length;
+
+ return this;
+ }
+
+ /// <summary>
+ /// Appends the specified <see cref="StringBuilder"/>.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ public ICharTermAttribute Append(StringBuilder value)
+ {
+ if (value == null)
+ return this.AppendNull();
+
+ return this.Append(value.ToString());
+ }
+
+ /// <summary>
+ /// Appends the specified <see cref="ICharTermAttribute"/>.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ public ICharTermAttribute Append(ICharTermAttribute value)
+ {
+ if (value == null)
+ return this.AppendNull();
+
+ int length = value.Length;
+
+ Array.Copy(value.Buffer, 0, this.ResizeBuffer(this.termLength + length), this.termLength, length);
+ this.termLength += length;
+
+ return this;
+ }
+
+
+ /// <summary>
+ /// Returns the character at the specified index index.
+ /// </summary>
+ /// <param name="index">The position of the character.</param>
+ /// <returns>An instance of <see cref="Char"/>.</returns>
+ /// <exception cref="IndexOutOfRangeException">
+ /// Throws when the index is equal or greater than the current buffer length
+ /// </exception>
+ public char CharAt(int index)
+ {
+ if (index >= this.termLength)
+ throw new IndexOutOfRangeException();
+
+ return this.Buffer[index];
+ }
+
+ /// <summary>
+ /// Clears the instance.
+ /// </summary>
+ public override void Clear()
+ {
+ this.termLength = 0;
+ }
+
+ /// <summary>
+ /// Creates a clone of the object, generally shallow.
+ /// </summary>
+ /// <returns>an the clone of the current instance.</returns>
+ public override object Clone()
+ {
+ CharTermAttribute clone = (CharTermAttribute)this.MemberwiseClone();
+
+ clone.Buffer = new char[this.termLength];
+ Array.Copy(this.Buffer, 0, clone.Buffer, 0, this.termLength);
+
+ return clone;
+ }
+
+ /// <summary>
+ /// Copies the contents of the buffer, starting at the offset to the specified length.
+ /// </summary>
+ /// <param name="buffer">The buffer to copy.</param>
+ /// <param name="offset">The index of the first character to copy inside the buffer.</param>
+ /// <param name="length">The number of characters to copy.</param>
+ public void CopyBuffer(char[] buffer, int offset = 0, int length = -1)
+ {
+ if (length == -1)
+ length = buffer.Length;
+
+ this.GrowBuffer(length);
+ Array.Copy(buffer, offset, this.Buffer, 0, length);
+ this.termLength = length;
+ }
+
+ /// <summary>
+ /// Copies to.
+ /// </summary>
+ /// <param name="attributeBase">The attribute base.</param>
+ public override void CopyTo(AttributeBase attributeBase)
+ {
+ CharTermAttribute attribute = (CharTermAttribute)attributeBase;
+ attribute.CopyBuffer(this.Buffer, 0, this.termLength);
+ }
+
+ /// <summary>
+ /// Sets the length of the internal buffer to zero. User this
+ /// method before appending content using <c>Append</c> methods.
+ /// </summary>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ public ICharTermAttribute Empty()
+ {
+ this.termLength = 0;
+ return this;
+ }
+
+
+ /// <summary>
+ /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
+ /// </summary>
+ /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
+ /// <returns>
+ /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool Equals(object obj)
+ {
+ if (this == obj)
+ return true;
+
+ if (obj is CharTermAttribute)
+ {
+ CharTermAttribute y = (CharTermAttribute)obj;
+
+ if (this.termLength != y.termLength)
+ return false;
+
+ for (int i = 0; i < this.termLength; i++)
+ {
+ if (this.Buffer[i] != y.Buffer[i])
+ return false;
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /// <summary>
+ /// Returns a hash code for this instance.
+ /// </summary>
+ /// <returns>
+ /// A hash code for this instance, suitable for use in
+ /// hashing algorithms and data structures like a hash table.
+ /// </returns>
+ public override int GetHashCode()
+ {
+ // TODO: implement ArrayUtil.HashCode();
+ int code = this.termLength;
+ code = code * 31;
+ return code;
+ }
+
+
+ /// <summary>
+ /// Resizes the length of the internal buffer to the new value and preserves the
+ /// existing content.
+ /// </summary>
+ /// <param name="length">The length to rebuffer to.</param>
+ /// <returns>The <see cref="T:System.Char"/> array.</returns>
+ public char[] ResizeBuffer(int length)
+ {
+ if (this.Buffer.Length < length)
+ {
+ char[] newBuffer = CreateBuffer(length);
+ Array.Copy(this.Buffer, 0, newBuffer, 0, this.Buffer.Length);
+ this.Buffer = newBuffer;
+ }
+
+ return this.Buffer;
+ }
+
+ /// <summary>
+ /// Gets or sets the number of valid characters, the length of the term, in
+ /// the termBuffer array.
+ /// </summary>
+ /// <param name="length">The length.</param>
+ /// <remarks>
+ /// <para>
+ /// Use this to truncate the termBuffer or to synchronize any external
+ /// manipulation of the termBuffer.
+ /// </para>
+ /// <note>
+ /// To grow the size of the array, use <see cref="ResizeBuffer(int)"/> first.
+ /// </note>
+ /// </remarks>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ public ICharTermAttribute SetLength(int length)
+ {
+ if (length > this.Buffer.Length)
+ {
+ string message = string.Format(
+ "The given length '{0}' needs to be less than current the internal buffer length '{1}'",
+ length,
+ this.Buffer.Length);
+
+ throw new ArgumentOutOfRangeException("length", message);
+ }
+
+ this.termLength = length;
+
+ return this;
+ }
+
+
+ /// <summary>
+ /// Returns a <see cref="System.String"/> that represents this instance.
+ /// </summary>
+ /// <returns>
+ /// A <see cref="System.String"/> that represents this instance.
+ /// </returns>
+ public override string ToString()
+ {
+ return new string(this.Buffer).Substring(0, this.termLength);
+ }
+
+ private static char[] CreateBuffer(int length)
+ {
+ return new char[ArrayUtil.Oversize(length, RamUsageEstimator.NumBytesChar)];
+ }
+
+ private CharTermAttribute AppendNull()
+ {
+ this.ResizeBuffer(this.termLength + 4);
+
+ this.Buffer[this.termLength++] = 'n';
+ this.Buffer[this.termLength++] = 'u';
+ this.Buffer[this.termLength++] = 'l';
+ this.Buffer[this.termLength++] = 'l';
+
+ return this;
+ }
+
+ private void GrowBuffer(int length)
+ {
+ if (this.Buffer.Length < length)
+ this.Buffer = CreateBuffer(length);
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/FlagsAttribute.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/FlagsAttribute.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/FlagsAttribute.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/FlagsAttribute.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,125 @@
+// -----------------------------------------------------------------------
+// <copyright file="FlagsAttribute.cs" company="Apache">
+//
+// 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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Analysis.TokenAttributes
+{
+ using System;
+ using Util;
+
+ // DOCS: enhance FlagsAttribute summary
+
+ /// <summary>
+ /// This attribute is used to pass different flags down the tokenizer chain. i.e. from one TokenFilter to another
+ /// TokenFilter.
+ /// </summary>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>Java File: </b> <a href="https://github.com/apache/lucene-solr/blob/trunk/lucene/src/java/org/apache/lucene/analysis/tokenattributes/FlagsAttributeImpl.java">
+ /// lucene/src/java/org/apache/lucene/analysis/tokenattributes/FlagsAttributeImpl.java
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Analysis/TokenAttributes/FlagsAttribute.cs">
+ /// src/Lucene.Net/Analysis/TokenAttributes/FlagsAttribute.cs
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# Tests: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/test/Lucene.Net.Test/Analysis/TokenAttributes/FlagsAttributeTest.cs">
+ /// test/Lucene.Net.Test/Analysis/TokenAttributes/FlagsAttributeTest.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// </remarks>
+ public class FlagsAttribute : AttributeBase, IFlagsAttribute
+ {
+ /// <summary>
+ /// Gets or sets the flags.
+ /// </summary>
+ /// <value>The flags.</value>
+ public int Flags { get; set; }
+
+ /// <summary>
+ /// Clears the instance of its flags.
+ /// </summary>
+ public override void Clear()
+ {
+ this.Flags = 0;
+ }
+
+ /// <summary>
+ /// Creates a clone of the object, generally shallow.
+ /// </summary>
+ /// <returns>an the clone of the current instance.</returns>
+ public override object Clone()
+ {
+ return new FlagsAttribute { Flags = this.Flags };
+ }
+
+
+ /// <summary>
+ /// Copies to the specified attribute.
+ /// </summary>
+ /// <param name="attributeBase">The <see cref="AttributeBase"/> object that is being copied to.</param>
+ /// <exception cref="ArgumentException">
+ /// Thrown when the <paramref name="attributeBase"/> is not an <see cref="FlagsAttribute"/>.
+ /// </exception>
+ public override void CopyTo(AttributeBase attributeBase)
+ {
+ if (!(attributeBase is FlagsAttribute))
+ throw new ArgumentException(
+ string.Format("attributeBase must be of type {0} in order to be copied", this.GetType().FullName),
+ "attributeBase");
+
+ FlagsAttribute attribute = (FlagsAttribute)attributeBase;
+ attribute.Flags = this.Flags;
+ }
+
+ /// <summary>
+ /// Returns a hash code for this instance.
+ /// </summary>
+ /// <returns>
+ /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
+ /// </returns>
+ public override int GetHashCode()
+ {
+ return this.Flags;
+ }
+
+ /// <summary>
+ /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
+ /// </summary>
+ /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
+ /// <returns>
+ /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool Equals(object obj)
+ {
+ if (this == obj)
+ return true;
+
+ if (obj is FlagsAttribute)
+ return ((FlagsAttribute)obj).Flags == this.Flags;
+
+ return false;
+ }
+ }
+}
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/ICharTermAttribute.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/ICharTermAttribute.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/ICharTermAttribute.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/ICharTermAttribute.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,178 @@
+// -----------------------------------------------------------------------
+// <copyright company="Apache" file="ICharTermAttribute.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>
+// -----------------------------------------------------------------------
+
+
+namespace Lucene.Net.Analysis.TokenAttributes
+{
+ using System.Text;
+ using Util;
+
+ /// <summary>
+ /// The term text of a Token.
+ /// </summary>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>Java File: </b> <a href="https://github.com/apache/lucene-solr/blob/trunk/lucene/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttribute.java">
+ /// lucene/src/java/org/apache/lucene/analysis/tokenattributes/CharTermAttribute.java
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Analysis/TokenAttributes/ICharTermAttribute.cs">
+ /// src/Lucene.Net/Analysis/TokenAttributes/ICharTermAttribute.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// </remarks>
+ public interface ICharTermAttribute : IAttribute
+ {
+ /// <summary>
+ /// Gets the internal termBuffer character array which you can then
+ /// directly alter.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// If the array is too small for the token, use <see cref="SetLength(int)"/>
+ /// to increase the size. After altering the buffer be sure to call
+ /// <see cref="SetLength(int)"/> to record the valid characters that
+ /// were placed into the termBuffer.
+ /// </para>
+ /// </remarks>
+ /// <value>The buffer.</value>
+ char[] Buffer { get; }
+
+ /// <summary>
+ /// Gets or sets the number of valid characters, the length of the term, in
+ /// the termBuffer array.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Use this to truncate the termBuffer or to synchronize any external
+ /// manipulation of the termBuffer.
+ /// </para>
+ /// <note>
+ /// To grow the size of the array, use <see cref="ResizeBuffer(int)"/> first.
+ /// </note>
+ /// </remarks>
+ int Length { get; set; }
+
+ /// <summary>
+ /// Appends the specified value.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ ICharTermAttribute Append(char value);
+
+ /// <summary>
+ /// Appends the specified <see cref="string"/> to internal buffer or character sequence.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ ICharTermAttribute Append(string value);
+
+
+ /// <summary>
+ /// Appends the specified <see cref="string"/> to internal buffer or character sequence.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <param name="startingIndex">the index of string to start the copy.</param>
+ /// <param name="length">The length of the string that is to be copied.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ ICharTermAttribute Append(string value, int startingIndex, int length);
+
+ /// <summary>
+ /// Appends the specified value.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ ICharTermAttribute Append(StringBuilder value);
+
+ /// <summary>
+ /// Appends the specified value.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ ICharTermAttribute Append(ICharTermAttribute value);
+
+ /// <summary>
+ /// Copies the contents of the buffer, starting at the offset to the specified length.
+ /// </summary>
+ /// <param name="buffer">The buffer to copy.</param>
+ /// <param name="offset">The index of the first character to copy inside the buffer.</param>
+ /// <param name="length">The number of characters to copy, if -1 the length will default to the buffer length.</param>
+ void CopyBuffer(char[] buffer, int offset = 0, int length = -1);
+
+
+ /// <summary>
+ /// Sets the length of the internal buffer to zero. User this
+ /// method before appending content using <c>Append</c> methods.
+ /// </summary>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ ICharTermAttribute Empty();
+
+
+ /// <summary>
+ /// Resizes the buffer.
+ /// </summary>
+ /// <param name="length">The length.</param>
+ /// <returns>An instance of <see cref="T:System.Char[]"/>.</returns>
+ char[] ResizeBuffer(int length);
+
+
+ /// <summary>
+ /// Gets or sets the number of valid characters, the length of the term, in
+ /// the termBuffer array.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Use this to truncate the termBuffer or to synchronize any external
+ /// manipulation of the termBuffer.
+ /// </para>
+ /// <note>
+ /// To grow the size of the array, use <see cref="ResizeBuffer(int)"/> first.
+ /// </note>
+ /// </remarks>
+ /// <param name="length">The length.</param>
+ /// <returns>
+ /// An instance of <see cref="ICharTermAttribute"/> for fluent interface
+ /// chaining purposes.
+ /// </returns>
+ ICharTermAttribute SetLength(int length);
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/IFlagsAttribute.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/IFlagsAttribute.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/IFlagsAttribute.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/IFlagsAttribute.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,51 @@
+// -----------------------------------------------------------------------
+// <copyright file="IFlagsAttribute.cs" company="Apache">
+//
+// 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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Analysis.TokenAttributes
+{
+ using Util;
+
+ /// <summary>
+ /// This is the contract for attributes that pass different flags down the tokenizer chain.
+ /// </summary>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>Java File: </b> <a href="https://github.com/apache/lucene-solr/blob/trunk/lucene/src/java/org/apache/lucene/analysis/tokenattributes/FlagsAttribute.java">
+ /// lucene/src/java/org/apache/lucene/analysis/tokenattributes/FlagsAttribute.java
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Analysis/TokenAttributes/IFlagsAttribute.cs">
+ /// src/Lucene.Net/Analysis/TokenAttributes/FlagsAttribute.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// </remarks>
+ public interface IFlagsAttribute : IAttribute
+ {
+ /// <summary>
+ /// Gets or sets the flags.
+ /// </summary>
+ /// <value>The flags.</value>
+ int Flags { get; set; }
+ }
+}
Added: 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=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.csproj (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.csproj Fri Aug 5 19:21:18 2011
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{07A5FF70-1EFE-4B0B-B338-5CA56C3A2826}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Lucene.Net</RootNamespace>
+ <AssemblyName>Lucene.Net</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkProfile>Profile2</TargetFrameworkProfile>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>..\..\build\bin\core\Debug\</OutputPath>
+ <DefineConstants>TRACE;DEBUG;SILVERLIGHT;WMP;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <DocumentationFile>..\..\build\bin\core\Debug\Lucene.Net.XML</DocumentationFile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System">
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Core">
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System.Net" />
+ <Reference Include="System.Runtime.Serialization" />
+ <Reference Include="System.ServiceModel" />
+ <Reference Include="System.Xml" />
+ <Reference Include="System.Xml.Serialization" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Analysis\TokenAttributes\CharTermAttribute.cs" />
+ <Compile Include="Analysis\TokenAttributes\ICharTermAttribute.cs" />
+ <Compile Include="Analysis\TokenAttributes\FlagsAttribute.cs" />
+ <Compile Include="Analysis\TokenAttributes\IFlagsAttribute.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Support\BaseDictionaryOfTKeyTValue.cs" />
+ <Compile Include="Support\ICloneable.cs" />
+ <Compile Include="Support\DictionaryExtensions.cs" />
+ <Compile Include="Support\WeakDictionaryOfTKeyTValue.cs" />
+ <Compile Include="Support\WeakKeyComparerOfT.cs" />
+ <Compile Include="Support\WeakKeyReferenceOfT.cs" />
+ <Compile Include="Support\WeakNullReferenceOfT.cs" />
+ <Compile Include="Support\WeakReferenceOfT.cs" />
+ <Compile Include="Util\ArrayUtil.cs" />
+ <Compile Include="Util\AttributeBase.cs" />
+ <Compile Include="Util\AttributeSource.cs" />
+ <Compile Include="Util\IAttribute.cs" />
+ <Compile Include="Util\RamUsageEstimator.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
+ <Import Condition="Exists('..\..\packages\StyleCop.4.5\StyleCop.targets')" Project="..\..\packages\StyleCop.4.5\StyleCop.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ No newline at end of file
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.sln
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.sln?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.sln (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Lucene.Net.sln Fri Aug 5 19:21:18 2011
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net", "Lucene.Net.csproj", "{07A5FF70-1EFE-4B0B-B338-5CA56C3A2826}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {07A5FF70-1EFE-4B0B-B338-5CA56C3A2826}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {07A5FF70-1EFE-4B0B-B338-5CA56C3A2826}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {07A5FF70-1EFE-4B0B-B338-5CA56C3A2826}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {07A5FF70-1EFE-4B0B-B338-5CA56C3A2826}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Properties/AssemblyInfo.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Properties/AssemblyInfo.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Properties/AssemblyInfo.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Properties/AssemblyInfo.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,53 @@
+// -----------------------------------------------------------------------
+// <copyright file="AssemblyInfo.cs" company="Apache">
+//
+// 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;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Lucene.Net")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("Lucene.Net")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+[assembly: CLSCompliant(true)]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Settings.StyleCop
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Settings.StyleCop?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Settings.StyleCop (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Settings.StyleCop Fri Aug 5 19:21:18 2011
@@ -0,0 +1,53 @@
+<StyleCopSettings Version="105">
+ <GlobalSettings>
+ <StringProperty Name="LinkedSettingsFile">..\..\build\scripts\rules.stylecop</StringProperty>
+ <StringProperty Name="MergeSettingsFiles">Linked</StringProperty>
+ </GlobalSettings>
+ <Analyzers>
+ <Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
+ <Rules>
+ <Rule Name="FileHeaderFileNameDocumentationMustMatchTypeName">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ <Rule Name="ElementsMustBeDocumented">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings />
+ </Analyzer>
+ <Analyzer AnalyzerId="StyleCop.CSharp.LayoutRules">
+ <Rules>
+ <Rule Name="CodeMustNotContainMultipleBlankLinesInARow">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ <Rule Name="CurlyBracketsMustNotBeOmitted">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ <Rule Name="CurlyBracketsForMultiLineStatementsMustNotShareLine">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings />
+ </Analyzer>
+ <Analyzer AnalyzerId="StyleCop.CSharp.SpacingRules">
+ <Rules>
+ <Rule Name="OperatorKeywordMustBeFollowedBySpace">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings />
+ </Analyzer>
+ </Analyzers>
+</StyleCopSettings>
\ No newline at end of file
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/BaseDictionaryOfTKeyTValue.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/BaseDictionaryOfTKeyTValue.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/BaseDictionaryOfTKeyTValue.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/BaseDictionaryOfTKeyTValue.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,477 @@
+// -----------------------------------------------------------------------
+// <copyright file="BaseDictionaryOfTKeyTValue.cs" company="Apache">
+//
+// 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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Support
+{
+ using System;
+ using System.Collections.Generic;
+
+
+
+#if !SILVERLIGHT
+ [DebuggerDisplay("Count = {Count}")]
+ [DebuggerTypeProxy(PREFIX + "DictionaryDebugView`2" + SUFFIX)]
+#endif
+
+ /// <summary>
+ /// The abstract type that helps to construct specialized dictionaries.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
+ /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Support/BaseDictionaryOfTKeyTValue.cs">
+ /// src/Lucene.Net/Support/BaseDictionaryOfTKeyTValue.cs
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# Tests: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/test/Lucene.Net.Test/Util/Support/WeakDictionaryOfTKeyTValueTest.cs">
+ /// test/Lucene.Net.Test/Support/WeakDictionaryOfTKeyTValueTest.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// </remarks>
+ public abstract class BaseDictionary<TKey, TValue> : IDictionary<TKey, TValue>
+ {
+ private KeyCollection keys;
+ private ValueCollection values;
+
+ /// <summary>
+ /// Gets the number of key pair values stored in the dictionary.
+ /// </summary>
+ public abstract int Count { get; }
+
+ /// <summary>
+ /// Gets a value indicating whether this instance is read only.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if this instance is read only; otherwise, <c>false</c>.
+ /// </value>
+ public bool IsReadOnly
+ {
+ get { return false; }
+ }
+
+ /// <summary>
+ /// Gets the keys.
+ /// </summary>
+ /// <value>The keys.</value>
+ public ICollection<TKey> Keys
+ {
+ get { return this.keys ?? (this.keys = new KeyCollection(this)); }
+ }
+
+ /// <summary>
+ /// Gets the values.
+ /// </summary>
+ /// <value>The values.</value>
+ public ICollection<TValue> Values
+ {
+ get { return this.values ?? (this.values = new ValueCollection(this)); }
+ }
+
+
+
+ /// <summary>
+ /// Gets or sets the <typeparamref name="TValue"/> with the specified key.
+ /// </summary>
+ /// <param name="key">The <typeparamref name="TKey"/> key mapped to a value.</param>
+ /// <returns>
+ /// The <typeparamref name="TValue"/> instance that is mapped to the <paramref name="key"/>
+ /// </returns>
+ /// <exception cref="KeyNotFoundException">
+ /// Thrown when the specified <paramref name="key"/> can not be found.
+ /// </exception>
+ public TValue this[TKey key]
+ {
+ get
+ {
+ TValue value;
+ if (!this.TryGetValue(key, out value))
+ throw new KeyNotFoundException();
+
+ return value;
+ }
+
+ set
+ {
+ this.SetValue(key, value);
+ }
+ }
+
+
+ /// <summary>
+ /// Adds the specified key and value to the dictionary.
+ /// </summary>
+ /// <param name="key">The key of the element to add.</param>
+ /// <param name="value">The value of the element to add.</param>
+ public abstract void Add(TKey key, TValue value);
+
+
+
+ /// <summary>
+ /// Adds the specified item.
+ /// </summary>
+ /// <param name="item">The item.</param>
+ public void Add(KeyValuePair<TKey, TValue> item)
+ {
+ this.Add(item.Key, item.Value);
+ }
+
+
+ /// <summary>
+ /// Removes all the key pair values from the dictionary and resets the count.
+ /// </summary>
+ public abstract void Clear();
+
+
+ /// <summary>
+ /// Determines whether [contains] [the specified item].
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <returns>
+ /// <c>true</c> if [contains] [the specified item]; otherwise, <c>false</c>.
+ /// </returns>
+ public bool Contains(KeyValuePair<TKey, TValue> item)
+ {
+ TValue value;
+ return this.TryGetValue(item.Key, out value) &&
+ EqualityComparer<TValue>.Default.Equals(value, item.Value);
+ }
+
+
+ /// <summary>
+ /// Determines whether the <see cref="BaseDictionary{TKey,TValue}"/> contains the specified key.
+ /// </summary>
+ /// <param name="key">The key to locate in the dictionary.</param>
+ /// <remarks>
+ /// <para>
+ /// This method approaches an O(1) operation.
+ /// </para>
+ /// </remarks>
+ /// <returns><c>true</c> if the <paramref name="key"/> was found, otherwise <c>false</c> </returns>
+ /// <exception cref="System.ArgumentNullException">Throw when the <paramref name="key"/> is null.</exception>
+ public abstract bool ContainsKey(TKey key);
+
+ /// <summary>
+ /// Copies to.
+ /// </summary>
+ /// <param name="array">The array.</param>
+ /// <param name="arrayIndex">Index of the array.</param>
+ public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
+ {
+ Copy(this, array, arrayIndex);
+ }
+
+
+ /// <summary>
+ /// Returns an enumerator that iterates through the <see cref="Dictionary{TKey,TValue}" />.
+ /// </summary>
+ /// <returns>A <see cref="IEnumerator{T}"/> of <see cref="KeyValuePair{TKey, TValue}"/> structure for the Dictionary.</returns>
+ public abstract IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator();
+
+
+ /// <summary>
+ /// Removes the specified key from <see cref="BaseDictionary{TKey,TValue}"/>.
+ /// </summary>
+ /// <param name="key">The key of the element to remove.</param>
+ /// <returns>
+ /// <c>true</c> if the element was successfully found and removed, otherwise <c>false</c>
+ /// </returns>
+ public abstract bool Remove(TKey key);
+
+ /// <summary>
+ /// Removes the specified item.
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <returns><c>true</c> if element was found and successfully removed; otherwise, <c>false</c>.</returns>
+ public bool Remove(KeyValuePair<TKey, TValue> item)
+ {
+ if (!this.Contains(item))
+ return false;
+
+ return this.Remove(item.Key);
+ }
+
+
+ /// <summary>
+ /// Attempts to the get value of the associated key.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// This method combines the functionality of the ContainsKey method and the Item property.
+ /// </para>
+ /// <para>
+ /// If the key is not found, then the value parameter gets the appropriate default value
+ /// for the value type TValue; for example, 0 (zero) for integer types, false for Boolean
+ /// types, and null for reference types.
+ /// </para>
+ /// <para>
+ /// Use the TryGetValue method if your code frequently attempts to access keys that are not
+ /// in the dictionary. Using this method is more efficient than catching the
+ /// KeyNotFoundException thrown by the Item property.
+ /// </para>
+ /// <para>
+ /// This method approaches an O(1) operation.
+ /// </para>
+ /// </remarks>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// <c>true</c>if the <see cref="BaseDictionary{TKey,TValue}"/> contains an element with the
+ /// specified key; otherwise, <c>false</c>.
+ /// </returns>
+ public abstract bool TryGetValue(TKey key, out TValue value);
+
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ {
+ return this.GetEnumerator();
+ }
+
+ /// <summary>
+ /// Sets the value.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ protected abstract void SetValue(TKey key, TValue value);
+
+
+
+ private static void Copy<T>(ICollection<T> source, T[] array, int arrayIndex)
+ {
+ if (array == null)
+ throw new ArgumentNullException("array");
+
+ if (arrayIndex < 0 || arrayIndex > array.Length)
+ throw new ArgumentOutOfRangeException("arrayIndex");
+
+ if ((array.Length - arrayIndex) < source.Count)
+ throw new ArgumentException("Destination array is not large enough. Check array.Length and arrayIndex.");
+
+ foreach (T item in source)
+ array[arrayIndex++] = item;
+ }
+
+
+
+ /// <summary>
+ /// The base class for providing the <typeparamref name="TKey"/> and <typeparamref name="TValue"/>
+ /// collections.
+ /// </summary>
+ /// <typeparam name="T">The <typeparamref name="T"/> type.</typeparam>
+ private abstract class Collection<T> : ICollection<T>
+ {
+ protected readonly IDictionary<TKey, TValue> Dictionary;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="BaseDictionary<TKey, TValue>.Collection<T>"/> class.
+ /// </summary>
+ /// <param name="dictionary">The dictionary.</param>
+ protected Collection(IDictionary<TKey, TValue> dictionary)
+ {
+ this.Dictionary = dictionary;
+ }
+
+
+
+ /// <summary>
+ /// Gets the count.
+ /// </summary>
+ /// <value>The count.</value>
+ public int Count
+ {
+ get { return this.Dictionary.Count; }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether this instance is read only.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if this instance is read only; otherwise, <c>false</c>.
+ /// </value>
+ public bool IsReadOnly
+ {
+ get { return true; }
+ }
+
+ /// <summary>
+ /// Adds the specified item.
+ /// </summary>
+ /// <param name="item">The item.</param>
+ public void Add(T item)
+ {
+ throw new NotSupportedException("Collection is read-only.");
+ }
+
+ /// <summary>
+ /// Clears this instance.
+ /// </summary>
+ public void Clear()
+ {
+ throw new NotSupportedException("Collection is read-only.");
+ }
+
+
+ /// <summary>
+ /// Determines whether [contains] [the specified item].
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <returns>
+ /// <c>true</c> if [contains] [the specified item]; otherwise, <c>false</c>.
+ /// </returns>
+ public virtual bool Contains(T item)
+ {
+ foreach (T element in this)
+ if (EqualityComparer<T>.Default.Equals(element, item))
+ return true;
+ return false;
+ }
+
+
+ /// <summary>
+ /// Copies to.
+ /// </summary>
+ /// <param name="array">The array.</param>
+ /// <param name="arrayIndex">Index of the array.</param>
+ public void CopyTo(T[] array, int arrayIndex)
+ {
+ Copy(this, array, arrayIndex);
+ }
+
+
+
+ /// <summary>
+ /// Returns an enumerator that iterates through the collection.
+ /// </summary>
+ /// <returns>An <see cref="IEnumerator{T}"/> of that can be used to iterate through the collection.</returns>
+ public IEnumerator<T> GetEnumerator()
+ {
+ foreach (KeyValuePair<TKey, TValue> pair in this.Dictionary)
+ yield return this.GetItem(pair);
+ }
+
+
+ /// <summary>
+ /// Removes the specified item.
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <returns>
+ /// <c>true</c> if the item was successfully removed; otherwise, <c>false</c>.
+ /// </returns>
+ /// <exception cref="NotSupportedException">
+ /// Throw when this method is called. This collection is read only.
+ /// </exception>
+ public bool Remove(T item)
+ {
+ throw new NotSupportedException("Collection is read-only.");
+ }
+
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ {
+ return this.GetEnumerator();
+ }
+
+
+ /// <summary>
+ /// Gets the item.
+ /// </summary>
+ /// <param name="pair">The pair.</param>
+ /// <returns>an instance of <typeparamref name="T"/>.</returns>
+ protected abstract T GetItem(KeyValuePair<TKey, TValue> pair);
+ }
+
+#if !SILVERLIGHT
+ [DebuggerDisplay("Count = {Count}")]
+ [DebuggerTypeProxy(PREFIX + "DictionaryKeyCollectionDebugView`2" + SUFFIX)]
+#endif
+ /// <summary>
+ /// The collection of dictionary keys.
+ /// </summary>
+ private class KeyCollection : Collection<TKey>
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="BaseDictionary<TKey, TValue>.KeyCollection"/> class.
+ /// </summary>
+ /// <param name="dictionary">The dictionary.</param>
+ public KeyCollection(IDictionary<TKey, TValue> dictionary)
+ : base(dictionary)
+ {
+ }
+
+
+ /// <summary>
+ /// Determines whether [contains] [the specified item].
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <returns>
+ /// <c>true</c> if [contains] [the specified item]; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool Contains(TKey item)
+ {
+ return this.Dictionary.ContainsKey(item);
+ }
+
+ /// <summary>
+ /// Gets the item.
+ /// </summary>
+ /// <param name="pair">The pair.</param>
+ /// <returns>
+ /// An instance of <typeparamref name="TKey"/>.
+ /// </returns>
+ protected override TKey GetItem(KeyValuePair<TKey, TValue> pair)
+ {
+ return pair.Key;
+ }
+ }
+
+#if !SILVERLIGHT
+ [DebuggerDisplay("Count = {Count}")]
+ [DebuggerTypeProxy(PREFIX + "DictionaryValueCollectionDebugView`2" + SUFFIX)]
+#endif
+ /// <summary>
+ /// The collection of values from the dictionary.
+ /// </summary>
+ private class ValueCollection : Collection<TValue>
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="BaseDictionary<TKey, TValue>.ValueCollection"/> class.
+ /// </summary>
+ /// <param name="dictionary">The dictionary.</param>
+ public ValueCollection(IDictionary<TKey, TValue> dictionary)
+ : base(dictionary)
+ {
+ }
+
+ /// <summary>
+ /// Gets the item.
+ /// </summary>
+ /// <param name="pair">The pair.</param>
+ /// <returns>
+ /// An instance of <typeparamref name="TValue"/>.
+ /// </returns>
+ protected override TValue GetItem(KeyValuePair<TKey, TValue> pair)
+ {
+ return pair.Value;
+ }
+ }
+ }
+}
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/DictionaryExtensions.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/DictionaryExtensions.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/DictionaryExtensions.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/DictionaryExtensions.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,61 @@
+// -----------------------------------------------------------------------
+// <copyright file="DictionaryExtensions.cs" company="Apache">
+//
+// 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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Support
+{
+ using System.Collections.Generic;
+
+ /// <summary>
+ /// Extension methods for <see cref="IDictionary{TKey,TValue}"/>
+ /// </summary>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Support/DictionaryExtensions.cs">
+ /// src/Lucene.Net/Support/DictionaryExtensions.cs
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# Tests: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/test/Lucene.Net.Test/Util/Support/DictionaryExtensionsTest.cs">
+ /// test/Lucene.Net.Test/Support/DictionaryExtensionsTest.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// </remarks>
+ public static class DictionaryExtensions
+ {
+ /// <summary>
+ /// Gets the associated value of the specified key if it exists, otherwise it returns <c>default(<typeparamref name="TValue"/>)</c>.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the key.</typeparam>
+ /// <typeparam name="TValue">The type of the value.</typeparam>
+ /// <param name="dictionary">The dictionary.</param>
+ /// <param name="key">The key.</param>
+ /// <returns>the <typeparamref name="TValue"/> or default value associated with the specified key.</returns>
+ public static TValue GetDefaultedValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
+ {
+ TValue value;
+ dictionary.TryGetValue(key, out value);
+
+ return value;
+ }
+ }
+}
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/ICloneable.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/ICloneable.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/ICloneable.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/ICloneable.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,44 @@
+// -----------------------------------------------------------------------
+// <copyright file="ICloneable.cs" company="Apache">
+//
+// 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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Support
+{
+ /// <summary>
+ /// Contract to ensure and object has <see cref="Clone()"/> method.
+ /// </summary>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Support/ICloneable.cs">
+ /// src/Lucene.Net/Support/ICloneable.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// </remarks>
+ public interface ICloneable
+ {
+ /// <summary>
+ /// Creates a clone of the object, generally shallow.
+ /// </summary>
+ /// <returns>an the clone of the current instance.</returns>
+ object Clone();
+ }
+}
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakDictionaryOfTKeyTValue.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakDictionaryOfTKeyTValue.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakDictionaryOfTKeyTValue.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakDictionaryOfTKeyTValue.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,324 @@
+// -----------------------------------------------------------------------
+// <copyright file="WeakDictionaryOfTKeyTValue.cs" company="Apache">
+//
+// 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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Support
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+
+ /// <summary>
+ /// A dictionary of weak references.
+ /// </summary>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Support/WeakDictionaryOfTKeyTValue.cs">
+ /// src/Lucene.Net/Support/WeakDictionaryOfTKeyTValue.cs
+ /// </a>
+ /// </para>
+ /// <para>
+ /// <b>C# Tests: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/test/Lucene.Net.Test/Util/Support/WeakDictionaryOfTKeyTValueTest.cs">
+ /// test/Lucene.Net.Test/Support/WeakDictionaryOfTKeyTValueTest.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// <para>
+ /// Heavily base on the implementation found here on
+ /// <a href="http://blogs.msdn.com/b/nicholg/archive/2006/06/04/617466.aspx"> Nick Guerrera's Blog</a>
+ /// </para>
+ /// <note>
+ /// This was implemented to be the C# equivalent of has a Java WeakHashMap.
+ /// </note>
+ /// </remarks>
+ /// <typeparam name="TKey">The <c>TKey</c> type.</typeparam>
+ /// <typeparam name="TValue">The <c>TValue</c> type.</typeparam>
+ public sealed class WeakDictionary<TKey, TValue> : BaseDictionary<TKey, TValue>
+ where TKey : class
+ where TValue : class
+ {
+ private Dictionary<object, WeakReference<TValue>> internalDictionary;
+ private WeakKeyComparer<TKey> comparer;
+ private int initialCapacity = 0;
+ private DateTime lastRemoval = DateTime.Now;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="WeakDictionary{TKey,TValue}"/> class.
+ /// </summary>
+ public WeakDictionary()
+ : this(0, null)
+ {
+ }
+
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="WeakDictionary<TKey, TValue>"/> class.
+ /// </summary>
+ /// <param name="capacity">The capacity.</param>
+ public WeakDictionary(int capacity)
+ : this(capacity, null)
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="WeakDictionary<TKey, TValue>"/> class.
+ /// </summary>
+ /// <param name="comparer">The comparer.</param>
+ public WeakDictionary(IEqualityComparer<TKey> comparer)
+ : this(0, comparer)
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="WeakDictionary<TKey, TValue>"/> class.
+ /// </summary>
+ /// <param name="capacity">The capacity.</param>
+ /// <param name="comparer">The comparer.</param>
+ public WeakDictionary(int capacity, IEqualityComparer<TKey> comparer)
+ {
+ this.PeriodicRemoval = new TimeSpan(0, 0, 15);
+ this.initialCapacity = capacity;
+ this.comparer = new WeakKeyComparer<TKey>(comparer);
+ this.internalDictionary = new Dictionary<object, WeakReference<TValue>>(capacity, this.comparer);
+ }
+
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="WeakDictionary<TKey, TValue>"/> class.
+ /// </summary>
+ /// <param name="dictionary">The dictionary.</param>
+ public WeakDictionary(IDictionary<TKey, TValue> dictionary)
+ : this(0, null)
+ {
+ foreach (var pair in dictionary)
+ this.Add(pair.Key, pair.Value);
+ }
+
+
+
+ /// <summary>
+ /// Gets the comparer.
+ /// </summary>
+ /// <value>The comparer.</value>
+ public IEqualityComparer<TKey> Comparer
+ {
+ get { return this.comparer.InternalComparer; }
+ }
+
+ // WARNING: The count returned here may include entries for which
+ // either the key or value objects have already been garbage
+ // collected. Call RemoveCollectedEntries to weed out collected
+ // entries and update the count accordingly.
+
+ /// <summary>
+ /// Gets the count.
+ /// </summary>
+ /// <value>The count.</value>
+ public override int Count
+ {
+ get
+ {
+ this.EnsureRemovalOfCollectedEntries();
+ return this.internalDictionary.Count;
+ }
+ }
+
+ /// <summary>
+ /// Gets the initial capacity.
+ /// </summary>
+ /// <value>The initial capacity.</value>
+ public int InitialCapacity
+ {
+ get { return this.initialCapacity; }
+ }
+
+ /// <summary>
+ /// Gets or sets the periodic removal.
+ /// </summary>
+ /// <value>The periodic removal.</value>
+ public TimeSpan PeriodicRemoval { get; set; }
+
+ /// <summary>
+ /// Adds the specified key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ public override void Add(TKey key, TValue value)
+ {
+ if (key == null)
+ throw new ArgumentNullException("key");
+
+ this.EnsureRemovalOfCollectedEntries();
+
+ WeakReference<TKey> weakKey = new WeakKeyReference<TKey>(key, this.comparer);
+ WeakReference<TValue> weakValue = new WeakReference<TValue>(value);
+
+ this.internalDictionary.Add(weakKey, weakValue);
+ }
+
+
+ /// <summary>
+ /// Clears this instance.
+ /// </summary>
+ public override void Clear()
+ {
+ this.internalDictionary.Clear();
+ }
+
+ /// <summary>
+ /// Determines whether the specified key contains key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <returns>
+ /// <c>true</c> if the specified key contains key; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool ContainsKey(TKey key)
+ {
+ this.EnsureRemovalOfCollectedEntries();
+ return this.internalDictionary.ContainsKey(key);
+ }
+
+ /// <summary>
+ /// Returns an enumerator that iterates through the collection.
+ /// </summary>
+ /// <returns>
+ /// An <see cref="IEnumerator{T}"/> of <see cref="KeyValuePair{TKey, TValue}"/> instance that
+ /// can be used to iterate through the collection.
+ /// </returns>
+ public override IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
+ {
+ this.EnsureRemovalOfCollectedEntries();
+
+ foreach (KeyValuePair<object, WeakReference<TValue>> pair in this.internalDictionary)
+ {
+ WeakReference<TKey> weakKey = (WeakReference<TKey>)pair.Key;
+ WeakReference<TValue> weakValue = pair.Value;
+
+ TKey key = weakKey.Target;
+ TValue value = weakValue.Target;
+
+ if (weakKey.IsAlive && weakValue.IsAlive)
+ yield return new KeyValuePair<TKey, TValue>(key, value);
+ }
+ }
+
+
+ /// <summary>
+ /// Removes the specified key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <returns>
+ /// <c>true</c> if the <see cref="KeyValuePair{TKey,TValue}"/> was successfully
+ /// removed; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool Remove(TKey key)
+ {
+ var result = this.internalDictionary.Remove(key);
+ this.EnsureRemovalOfCollectedEntries();
+ return result;
+ }
+
+ // Removes the left-over weak references for entries in the dictionary
+ // whose key or value has already been reclaimed by the garbage
+ // collector. This will reduce the dictionary's Count by the number
+ // of dead key-value pairs that were eliminated.
+
+
+
+ /// <summary>
+ /// Removes the collected entries.
+ /// </summary>
+ public void RemoveCollectedEntries()
+ {
+ List<object> list = null;
+
+ foreach (KeyValuePair<object, WeakReference<TValue>> pair in this.internalDictionary)
+ {
+ WeakReference<TKey> weakKey = (WeakReference<TKey>)pair.Key;
+ WeakReference<TValue> weakValue = pair.Value;
+
+ if (!weakKey.IsAlive || !weakValue.IsAlive)
+ {
+ if (list == null)
+ list = new List<object>();
+
+ list.Add(weakKey);
+ }
+ }
+
+ if (list != null)
+ {
+ foreach (object key in list)
+ this.internalDictionary.Remove(key);
+ }
+ }
+
+ /// <summary>
+ /// Tries the get value.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// <c>true</c> if the <paramref name="key"/> was successfully
+ /// found; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool TryGetValue(TKey key, out TValue value)
+ {
+ this.EnsureRemovalOfCollectedEntries();
+
+ WeakReference<TValue> weakValue;
+ if (this.internalDictionary.TryGetValue(key, out weakValue))
+ {
+ value = weakValue.Target;
+ return weakValue.IsAlive;
+ }
+
+ value = default(TValue);
+ return false;
+ }
+
+
+
+ /// <summary>
+ /// Sets the value.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ protected override void SetValue(TKey key, TValue value)
+ {
+ WeakReference<TKey> weakKey = new WeakKeyReference<TKey>(key, this.comparer);
+ this.internalDictionary[weakKey] = new WeakReference<TValue>(value);
+ }
+
+ /// <summary>
+ /// Ensures the removal of collected entries.
+ /// </summary>
+ private void EnsureRemovalOfCollectedEntries()
+ {
+ if (this.PeriodicRemoval != null && this.lastRemoval.Add(this.PeriodicRemoval) < DateTime.Now)
+ {
+ this.RemoveCollectedEntries();
+ this.lastRemoval = DateTime.Now;
+ }
+ }
+ }
+}
Added: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakKeyComparerOfT.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakKeyComparerOfT.cs?rev=1154359&view=auto
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakKeyComparerOfT.cs (added)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Support/WeakKeyComparerOfT.cs Fri Aug 5 19:21:18 2011
@@ -0,0 +1,134 @@
+// -----------------------------------------------------------------------
+// <copyright file="WeakKeyComparerOfT.cs" company="Apache">
+//
+// 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>
+// -----------------------------------------------------------------------
+
+namespace Lucene.Net.Support
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+
+ /// <summary>
+ /// TODO: Summary
+ /// </summary>
+ /// <typeparam name="T">The type for the comparers of the TKey Type for the Dictionary.</typeparam>
+ /// <remarks>
+ /// <note>
+ /// <para>
+ /// <b>C# File: </b> <a href="https://github.com/wickedsoftware/lucene.net/tree/lucene-net-4/src/Lucene.Net/Support/WeakKeyComparerOfT.cs">
+ /// src/Lucene.Net/Support/WeakKeyComparerOfT.cs
+ /// </a>
+ /// </para>
+ /// </note>
+ /// </remarks>
+ internal sealed class WeakKeyComparer<T> : IEqualityComparer<object>
+ where T : class
+ {
+ private IEqualityComparer<T> comparer;
+
+ internal WeakKeyComparer(IEqualityComparer<T> comparer)
+ {
+ if (comparer == null)
+ comparer = EqualityComparer<T>.Default;
+
+ this.comparer = comparer;
+ }
+
+ /// <summary>
+ /// Gets the internal comparer.
+ /// </summary>
+ /// <value>The internal comparer.</value>
+ public IEqualityComparer<T> InternalComparer
+ {
+ get { return this.comparer; }
+ }
+
+ /// <summary>
+ /// Returns a hash code for this instance.
+ /// </summary>
+ /// <param name="obj">The obj.</param>
+ /// <returns>
+ /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
+ /// </returns>
+ public int GetHashCode(object obj)
+ {
+ WeakKeyReference<T> weakKey = obj as WeakKeyReference<T>;
+
+ if (weakKey != null)
+ return weakKey.HashCode;
+
+ return this.comparer.GetHashCode((T)obj);
+ }
+
+ // Note: There are actually 9 cases to handle here.
+ //
+ // Let Wa = Alive Weak Reference
+ // Let Wd = Dead Weak Reference
+ // Let S = Strong Reference
+ //
+ // x | y | Equals(x,y)
+ // -------------------------------------------------
+ // Wa | Wa | comparer.Equals(x.Target, y.Target)
+ // Wa | Wd | false
+ // Wa | S | comparer.Equals(x.Target, y)
+ // Wd | Wa | false
+ // Wd | Wd | x == y
+ // Wd | S | false
+ // S | Wa | comparer.Equals(x, y.Target)
+ // S | Wd | false
+ // S | S | comparer.Equals(x, y)
+ // -------------------------------------------------
+ public new bool Equals(object x, object y)
+ {
+ bool parameterXIsDead, parameterYIsDead;
+
+ T first = GetTarget(x, out parameterXIsDead);
+ T second = GetTarget(y, out parameterYIsDead);
+
+ if (parameterXIsDead)
+ return parameterYIsDead ? x == y : false;
+
+ if (parameterYIsDead)
+ return false;
+
+ return this.comparer.Equals(first, second);
+ }
+
+ private static T GetTarget(object obj, out bool isDead)
+ {
+ WeakKeyReference<T> weakreference = obj as WeakKeyReference<T>;
+ T target;
+
+ if (weakreference != null)
+ {
+ target = weakreference.Target;
+ isDead = !weakreference.IsAlive;
+ }
+ else
+ {
+ target = (T)obj;
+ isDead = false;
+ }
+
+ return target;
+ }
+ }
+}