19 using Lucene.Net.Analysis.Tokenattributes;
20 using Lucene.Net.Support;
21 using Lucene.Net.Util;
27 namespace Lucene.Net.Analysis
120 public const String DEFAULT_TYPE =
"word";
122 private const int MIN_BUFFER_SIZE = 10;
124 private char[] termBuffer;
125 private int termLength;
126 private int startOffset, endOffset;
127 private string type = DEFAULT_TYPE;
130 private int positionIncrement = 1;
154 public Token(
int start,
int end, String typ)
167 public Token(
int start,
int end,
int flags)
183 public Token(String text,
int start,
int end)
199 public Token(System.String text,
int start,
int end, System.String typ)
216 public Token(System.String text,
int start,
int end,
int flags)
233 public Token(
char[] startTermBuffer,
int termBufferOffset,
int termBufferLength,
int start,
int end)
235 SetTermBuffer(startTermBuffer, termBufferOffset, termBufferLength);
267 public virtual int PositionIncrement
272 throw new System.ArgumentException(
"Increment must be zero or greater: " + value);
273 this.positionIncrement = value;
275 get {
return positionIncrement; }
292 return new System.String(termBuffer, 0, termLength);
302 public void SetTermBuffer(
char[] buffer,
int offset,
int length)
304 GrowTermBuffer(length);
305 Array.Copy(buffer, offset, termBuffer, 0, length);
312 public void SetTermBuffer(System.String buffer)
314 int length = buffer.Length;
315 GrowTermBuffer(length);
329 public void SetTermBuffer(System.String buffer,
int offset,
int length)
331 System.Diagnostics.Debug.Assert(offset <= buffer.Length);
332 System.Diagnostics.Debug.Assert(offset + length <= buffer.Length);
333 GrowTermBuffer(length);
346 public char[] TermBuffer()
364 public virtual char[] ResizeTermBuffer(
int newSize)
366 if (termBuffer == null)
368 termBuffer =
new char[
ArrayUtil.
GetNextSize(newSize < MIN_BUFFER_SIZE ? MIN_BUFFER_SIZE : newSize)];
372 if (termBuffer.Length < newSize)
377 Array.Copy(termBuffer, 0, newCharBuffer, 0, termBuffer.Length);
378 termBuffer = newCharBuffer;
389 private void GrowTermBuffer(
int newSize)
391 if (termBuffer == null)
394 termBuffer =
new char[
ArrayUtil.
GetNextSize(newSize < MIN_BUFFER_SIZE?MIN_BUFFER_SIZE:newSize)];
398 if (termBuffer.Length < newSize)
407 private void InitTermBuffer()
409 if (termBuffer == null)
419 public int TermLength()
433 public void SetTermLength(
int length)
436 if (length > termBuffer.Length)
437 throw new System.ArgumentException(
"length " + length +
" exceeds the size of the termBuffer (" + termBuffer.Length +
")");
447 public virtual int StartOffset
449 get {
return startOffset; }
450 set { this.startOffset = value; }
457 public virtual int EndOffset
459 get {
return endOffset; }
460 set { this.endOffset = value; }
466 public virtual void SetOffset(
int startOffset,
int endOffset)
468 this.startOffset = startOffset;
469 this.endOffset = endOffset;
476 set { this.type = value; }
488 public virtual int Flags
490 get {
return flags; }
491 set { flags = value; }
497 get {
return payload; }
498 set { payload = value; }
501 public override String ToString()
503 var sb =
new System.Text.StringBuilder();
506 if (termBuffer == null)
509 sb.Append(termBuffer, 0, termLength);
510 sb.Append(
',').Append(startOffset).Append(
',').Append(endOffset);
511 if (!type.Equals(
"word"))
512 sb.Append(
",type=").Append(type);
513 if (positionIncrement != 1)
514 sb.Append(
",posIncr=").Append(positionIncrement);
516 return sb.ToString();
522 public override void Clear()
527 positionIncrement = 1;
529 startOffset = endOffset = 0;
533 public override System.Object Clone()
535 var t = (
Token) base.Clone();
537 if (termBuffer != null)
539 t.termBuffer =
new char[termBuffer.Length];
540 termBuffer.CopyTo(t.termBuffer, 0);
544 t.payload = (
Payload) payload.Clone();
555 public virtual Token Clone(
char[] newTermBuffer,
int newTermOffset,
int newTermLength,
int newStartOffset,
int newEndOffset)
557 var t =
new Token(newTermBuffer, newTermOffset, newTermLength, newStartOffset, newEndOffset)
558 {positionIncrement = positionIncrement, flags = flags, type = type};
560 t.payload = (
Payload) payload.Clone();
564 public override bool Equals(Object obj)
569 var other = obj as
Token;
574 other.InitTermBuffer();
576 if (termLength == other.termLength && startOffset == other.startOffset && endOffset == other.endOffset &&
577 flags == other.flags && positionIncrement == other.positionIncrement && SubEqual(type, other.type) &&
578 SubEqual(payload, other.payload))
580 for (
int i = 0; i < termLength; i++)
581 if (termBuffer[i] != other.termBuffer[i])
588 private bool SubEqual(System.Object o1, System.Object o2)
592 return o1.Equals(o2);
595 public override int GetHashCode()
598 int code = termLength;
599 code = code * 31 + startOffset;
600 code = code * 31 + endOffset;
601 code = code * 31 + flags;
602 code = code * 31 + positionIncrement;
603 code = code * 31 + type.GetHashCode();
604 code = (payload == null?code:code * 31 + payload.GetHashCode());
610 private void ClearNoTermBuffer()
613 positionIncrement = 1;
615 startOffset = endOffset = 0;
627 public virtual Token Reinit(
char[] newTermBuffer,
int newTermOffset,
int newTermLength,
int newStartOffset,
int newEndOffset, System.String newType)
631 positionIncrement = 1;
632 SetTermBuffer(newTermBuffer, newTermOffset, newTermLength);
633 startOffset = newStartOffset;
634 endOffset = newEndOffset;
647 public virtual Token Reinit(
char[] newTermBuffer,
int newTermOffset,
int newTermLength,
int newStartOffset,
int newEndOffset)
650 SetTermBuffer(newTermBuffer, newTermOffset, newTermLength);
651 startOffset = newStartOffset;
652 endOffset = newEndOffset;
665 public virtual Token Reinit(System.String newTerm,
int newStartOffset,
int newEndOffset, System.String newType)
668 SetTermBuffer(newTerm);
669 startOffset = newStartOffset;
670 endOffset = newEndOffset;
683 public virtual Token Reinit(System.String newTerm,
int newTermOffset,
int newTermLength,
int newStartOffset,
int newEndOffset, System.String newType)
686 SetTermBuffer(newTerm, newTermOffset, newTermLength);
687 startOffset = newStartOffset;
688 endOffset = newEndOffset;
701 public virtual Token Reinit(System.String newTerm,
int newStartOffset,
int newEndOffset)
704 SetTermBuffer(newTerm);
705 startOffset = newStartOffset;
706 endOffset = newEndOffset;
719 public virtual Token Reinit(System.String newTerm,
int newTermOffset,
int newTermLength,
int newStartOffset,
int newEndOffset)
722 SetTermBuffer(newTerm, newTermOffset, newTermLength);
723 startOffset = newStartOffset;
724 endOffset = newEndOffset;
732 public virtual void Reinit(
Token prototype)
734 prototype.InitTermBuffer();
735 SetTermBuffer(prototype.termBuffer, 0, prototype.termLength);
736 positionIncrement = prototype.positionIncrement;
737 flags = prototype.flags;
738 startOffset = prototype.startOffset;
739 endOffset = prototype.endOffset;
740 type = prototype.type;
741 payload = prototype.payload;
749 public virtual void Reinit(
Token prototype, System.String newTerm)
751 SetTermBuffer(newTerm);
752 positionIncrement = prototype.positionIncrement;
753 flags = prototype.flags;
754 startOffset = prototype.startOffset;
755 endOffset = prototype.endOffset;
756 type = prototype.type;
757 payload = prototype.payload;
769 public virtual void Reinit(
Token prototype,
char[] newTermBuffer,
int offset,
int length)
771 SetTermBuffer(newTermBuffer, offset, length);
772 positionIncrement = prototype.positionIncrement;
773 flags = prototype.flags;
774 startOffset = prototype.startOffset;
775 endOffset = prototype.endOffset;
776 type = prototype.type;
777 payload = prototype.payload;
784 var to = (
Token) target;
795 ((
ITermAttribute) target).SetTermBuffer(termBuffer, 0, termLength);
828 this._delegateFactory = delegateFactory;
833 return typeof(T).IsAssignableFrom(typeof(
Token))
835 : _delegateFactory.CreateAttributeInstance<T>();
838 public override bool Equals(Object other)
840 if (
this == other)
return true;
843 return af != null && _delegateFactory.
Equals(af._delegateFactory);
846 public override int GetHashCode()
848 return _delegateFactory.GetHashCode() ^ 0x0a45aa31;