gitlab.com/evatix-go/core@v1.3.55/coredata/corerange/RangeInt16.go (about)

     1  package corerange
     2  
     3  import (
     4  	"math"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  )
     8  
     9  type RangeInt16 struct {
    10  	*BaseRange
    11  	Start, End int16
    12  }
    13  
    14  func NewRangeInt16MinMax(
    15  	rawString, separator string,
    16  	min, max int16,
    17  ) *RangeInt16 {
    18  	minMax := MinMaxInt{
    19  		Min: int(min),
    20  		Max: int(max),
    21  	}
    22  
    23  	return NewRangeInt(
    24  		rawString,
    25  		separator,
    26  		&minMax).
    27  		CreateRangeInt16()
    28  }
    29  
    30  func NewRangeInt16(
    31  	rawString, separator string,
    32  	minMaxInt16 *MinMaxInt16,
    33  ) *RangeInt16 {
    34  	if minMaxInt16 == nil {
    35  		minMax := &MinMaxInt{
    36  			Min: math.MinInt16,
    37  			Max: math.MaxInt16,
    38  		}
    39  
    40  		rangeInt := NewRangeInt(
    41  			rawString,
    42  			separator,
    43  			minMax)
    44  
    45  		return rangeInt.CreateRangeInt16()
    46  	}
    47  
    48  	minMax := minMaxInt16.CreateMinMaxInt()
    49  
    50  	rangeInt := NewRangeInt(
    51  		rawString,
    52  		separator,
    53  		minMax)
    54  
    55  	return rangeInt.CreateRangeInt16()
    56  }
    57  
    58  func (r *RangeInt16) Difference() int16 {
    59  	return r.End - r.Start
    60  }
    61  
    62  func (r *RangeInt16) DifferenceAbsolute() int16 {
    63  	diff := r.Difference()
    64  
    65  	if diff < 0 {
    66  		return diff
    67  	}
    68  
    69  	return diff
    70  }
    71  
    72  // RangeLength (5 - 3 = 2) + 1
    73  func (r *RangeInt16) RangeLength() int16 {
    74  	return r.DifferenceAbsolute() + 1
    75  }
    76  
    77  // RangesInt16 returns empty ints if IsInvalid
    78  // return range int values
    79  func (r *RangeInt16) RangesInt16() *[]int16 {
    80  	return r.Ranges()
    81  }
    82  
    83  // Ranges returns empty ints if IsInvalid
    84  // return range int values
    85  func (r *RangeInt16) Ranges() *[]int16 {
    86  	if r.IsInvalid() {
    87  		return &[]int16{}
    88  	}
    89  
    90  	length := r.RangeLength()
    91  	start := r.Start
    92  	slice := make([]int16, constants.Zero, length)
    93  
    94  	var i int16
    95  
    96  	for i = 0; i < length; i++ {
    97  		slice[i] = start + i
    98  	}
    99  
   100  	return &slice
   101  }
   102  
   103  func (r *RangeInt16) String() string {
   104  	return r.BaseRange.String(r.Start, r.End)
   105  }
   106  
   107  // IsWithinRange r.Start >= value && value <= r.End
   108  func (r *RangeInt16) IsWithinRange(value int16) bool {
   109  	return r.Start >= value && value <= r.End
   110  }
   111  
   112  // IsValidPlusWithinRange r.IsValid && r.IsWithinRange(value)
   113  func (r *RangeInt16) IsValidPlusWithinRange(value int16) bool {
   114  	return r.IsValid && r.IsWithinRange(value)
   115  }
   116  
   117  // IsInvalidValue !r.IsValid || !r.IsWithinRange(value)
   118  func (r *RangeInt16) IsInvalidValue(value int16) bool {
   119  	return !r.IsValid || !r.IsWithinRange(value)
   120  }