github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/analysis/tokenattributes/packedToken.go (about)

     1  package tokenattributes
     2  
     3  import (
     4  	"github.com/balzaczyy/golucene/core/util"
     5  )
     6  
     7  /*
     8  Default implementation of the common attributes used by Lucene:
     9  - CharTermAttribute
    10  - TypeAttribute
    11  - PositionIncrementAttribute
    12  - PositionLengthAttribute
    13  - OffsetAttribute
    14  */
    15  type PackedTokenAttributeImpl struct {
    16  	*CharTermAttributeImpl
    17  	startOffset, endOffset int
    18  	typ                    string
    19  	positionIncrement      int
    20  	positionLength         int
    21  }
    22  
    23  func NewPackedTokenAttribute() util.AttributeImpl {
    24  	return &PackedTokenAttributeImpl{
    25  		CharTermAttributeImpl: newCharTermAttributeImpl(),
    26  		typ:               DEFAULT_TYPE,
    27  		positionIncrement: 1,
    28  		positionLength:    1,
    29  	}
    30  }
    31  
    32  func (a *PackedTokenAttributeImpl) Interfaces() []string {
    33  	return []string{"CharTermAttribute", "TermToBytesRefAttribute",
    34  		"TypeAttribute", "PositionIncrementAttribute",
    35  		"PositionLengthAttribute", "OffsetAttribute"}
    36  }
    37  
    38  func (a *PackedTokenAttributeImpl) SetPositionIncrement(positionIncrement int) {
    39  	assert2(positionIncrement >= 0, "Increment must be zero or greater: %v", positionIncrement)
    40  	a.positionIncrement = positionIncrement
    41  }
    42  
    43  func (a *PackedTokenAttributeImpl) PositionIncrement() int {
    44  	return a.positionIncrement
    45  }
    46  
    47  func (a *PackedTokenAttributeImpl) StartOffset() int {
    48  	return a.startOffset
    49  }
    50  
    51  func (a *PackedTokenAttributeImpl) EndOffset() int {
    52  	return a.endOffset
    53  }
    54  
    55  func (a *PackedTokenAttributeImpl) SetOffset(startOffset, endOffset int) {
    56  	assert2(startOffset >= 0 && startOffset <= endOffset,
    57  		"startOffset must be non-negative, and endOffset must be >= startOffset, "+
    58  			"startOffset=%v,endOffset=%v", startOffset, endOffset)
    59  	a.startOffset = startOffset
    60  	a.endOffset = endOffset
    61  }
    62  
    63  func (a *PackedTokenAttributeImpl) SetType(typ string) {
    64  	a.typ = typ
    65  }
    66  
    67  func (a *PackedTokenAttributeImpl) Clear() {
    68  	a.CharTermAttributeImpl.Clear()
    69  	a.positionIncrement, a.positionLength = 1, 1
    70  	a.startOffset, a.endOffset = 0, 0
    71  	a.typ = DEFAULT_TYPE
    72  }
    73  
    74  func (a *PackedTokenAttributeImpl) Clone() util.AttributeImpl {
    75  	return &PackedTokenAttributeImpl{
    76  		CharTermAttributeImpl: a.CharTermAttributeImpl.Clone().(*CharTermAttributeImpl),
    77  		startOffset:           a.startOffset,
    78  		endOffset:             a.endOffset,
    79  		typ:                   a.typ,
    80  		positionIncrement:     a.positionIncrement,
    81  		positionLength:        a.positionLength,
    82  	}
    83  }
    84  
    85  func (a *PackedTokenAttributeImpl) CopyTo(target util.AttributeImpl) {
    86  	if to, ok := target.(*PackedTokenAttributeImpl); ok {
    87  		to.CopyBuffer(a.Buffer()[:a.Length()])
    88  		to.positionIncrement = a.positionIncrement
    89  		to.positionLength = a.positionLength
    90  		to.startOffset = a.startOffset
    91  		to.endOffset = a.endOffset
    92  		to.typ = a.typ
    93  	} else {
    94  		a.CharTermAttributeImpl.CopyTo(target)
    95  		target.(OffsetAttribute).SetOffset(a.startOffset, a.endOffset)
    96  		target.(PositionIncrementAttribute).SetPositionIncrement(a.positionIncrement)
    97  		target.(PositionLengthAttribute).SetPositionLength(a.positionLength)
    98  		target.(TypeAttribute).SetType(a.typ)
    99  	}
   100  }