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

     1  package corerange
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  	"gitlab.com/evatix-go/core/converters"
     9  	"gitlab.com/evatix-go/core/coreindexes"
    10  )
    11  
    12  type RangeInt struct {
    13  	*BaseRange
    14  	Start, End int
    15  }
    16  
    17  func NewRangeIntUsingValues(
    18  	start, end int,
    19  	isValid bool,
    20  ) *RangeInt {
    21  	return &RangeInt{
    22  		BaseRange: &BaseRange{
    23  			RawInput:  strconv.Itoa(start) + defaultSeparator + strconv.Itoa(end),
    24  			Separator: defaultSeparator,
    25  			IsValid:   isValid,
    26  			HasStart:  start > 0,
    27  			HasEnd:    end > 0,
    28  		},
    29  		Start: start,
    30  		End:   end,
    31  	}
    32  }
    33  
    34  func NewRangeIntMinMax(
    35  	rawString, separator string,
    36  	min, max int,
    37  ) *RangeInt {
    38  	minMax := MinMaxInt{
    39  		Min: min,
    40  		Max: max,
    41  	}
    42  
    43  	return NewRangeInt(rawString, separator, &minMax)
    44  }
    45  
    46  // NewRangeInt : MinMaxInt represent no validation on start and end range.
    47  func NewRangeInt(
    48  	rawString, separator string,
    49  	minMax *MinMaxInt,
    50  ) *RangeInt {
    51  	ranges := strings.Split(rawString, separator)
    52  	length := len(ranges)
    53  	hasStart := length >= 1
    54  	hasEnd := length >= 2
    55  	isValid := false
    56  	var start, end int
    57  
    58  	if hasStart {
    59  		start, isValid = converters.StringToIntegerWithDefault(
    60  			ranges[coreindexes.First],
    61  			constants.MaxInt)
    62  	}
    63  
    64  	if hasEnd {
    65  		end, isValid = converters.StringToIntegerWithDefault(
    66  			ranges[coreindexes.Second],
    67  			constants.MinInt)
    68  	}
    69  
    70  	isValid = isValid &&
    71  		length == 2 &&
    72  		hasStart &&
    73  		hasEnd &&
    74  		end > start
    75  
    76  	if minMax != nil {
    77  		isValid = isValid &&
    78  			start >= minMax.Min &&
    79  			end <= minMax.Max
    80  
    81  		return &RangeInt{
    82  			BaseRange: &BaseRange{
    83  				RawInput:  rawString,
    84  				Separator: separator,
    85  				IsValid:   isValid,
    86  				HasStart:  hasStart,
    87  				HasEnd:    hasEnd,
    88  			},
    89  			Start: start,
    90  			End:   end,
    91  		}
    92  	}
    93  
    94  	return &RangeInt{
    95  		BaseRange: &BaseRange{
    96  			RawInput:  rawString,
    97  			Separator: separator,
    98  			IsValid:   isValid,
    99  			HasStart:  hasStart,
   100  			HasEnd:    hasEnd,
   101  		},
   102  		Start: start,
   103  		End:   end,
   104  	}
   105  }
   106  
   107  func (r *RangeInt) Difference() int {
   108  	return r.End - r.Start
   109  }
   110  
   111  func (r *RangeInt) DifferenceAbsolute() int {
   112  	diff := r.Difference()
   113  
   114  	if diff < 0 {
   115  		return diff
   116  	}
   117  
   118  	return diff
   119  }
   120  
   121  // RangeLength (5 - 3 = 2) + 1
   122  func (r *RangeInt) RangeLength() int {
   123  	return r.DifferenceAbsolute() + 1
   124  }
   125  
   126  // RangesInt returns empty ints if IsInvalid
   127  // return range int values
   128  func (r *RangeInt) RangesInt() *[]int {
   129  	return r.Ranges()
   130  }
   131  
   132  // Ranges returns empty ints if IsInvalid
   133  // return range int values
   134  func (r *RangeInt) Ranges() *[]int {
   135  	if r.IsInvalid() {
   136  		return &[]int{}
   137  	}
   138  
   139  	length := r.RangeLength()
   140  	start := r.Start
   141  	slice := make([]int, constants.Zero, length)
   142  
   143  	for i := 0; i < length; i++ {
   144  		slice[i] = start + i
   145  	}
   146  
   147  	return &slice
   148  }
   149  
   150  func (r *RangeInt) String() string {
   151  	return r.BaseRange.String(r.Start, r.End)
   152  }
   153  
   154  func (r *RangeInt) CreateRangeInt8() *RangeInt8 {
   155  	return &RangeInt8{
   156  		BaseRange: r.BaseRangeClone(),
   157  		Start:     int8(r.Start),
   158  		End:       int8(r.End),
   159  	}
   160  }
   161  
   162  func (r *RangeInt) CreateRangeByte() *RangeByte {
   163  	return &RangeByte{
   164  		BaseRange: r.BaseRangeClone(),
   165  		Start:     byte(r.Start),
   166  		End:       byte(r.End),
   167  	}
   168  }
   169  
   170  func (r *RangeInt) CreateRangeInt16() *RangeInt16 {
   171  	return &RangeInt16{
   172  		BaseRange: r.BaseRangeClone(),
   173  		Start:     int16(r.Start),
   174  		End:       int16(r.End),
   175  	}
   176  }
   177  
   178  func (r *RangeInt) ShallowCreateRangeInt16() *RangeInt16 {
   179  	return &RangeInt16{
   180  		BaseRange: r.BaseRange,
   181  		Start:     int16(r.Start),
   182  		End:       int16(r.End),
   183  	}
   184  }
   185  
   186  func (r *RangeInt) ShallowCreateRangeInt8() *RangeInt8 {
   187  	return &RangeInt8{
   188  		BaseRange: r.BaseRange,
   189  		Start:     int8(r.Start),
   190  		End:       int8(r.End),
   191  	}
   192  }
   193  
   194  func (r *RangeInt) ShallowCreateRangeByte() *RangeByte {
   195  	return &RangeByte{
   196  		BaseRange: r.BaseRange,
   197  		Start:     byte(r.Start),
   198  		End:       byte(r.End),
   199  	}
   200  }
   201  
   202  // IsWithinRange r.Start >= value && value <= r.End
   203  func (r *RangeInt) IsWithinRange(value int) bool {
   204  	return r.Start >= value && value <= r.End
   205  }
   206  
   207  // IsValidPlusWithinRange r.IsValid && r.IsWithinRange(value)
   208  func (r *RangeInt) IsValidPlusWithinRange(value int) bool {
   209  	return r.IsValid && r.IsWithinRange(value)
   210  }
   211  
   212  // IsInvalidValue !r.IsValid || !r.IsWithinRange(value)
   213  func (r *RangeInt) IsInvalidValue(value int) bool {
   214  	return !r.IsValid || !r.IsWithinRange(value)
   215  }