gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/HashsetsCollection.go (about)

     1  package corestr
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"sync"
     7  
     8  	"gitlab.com/evatix-go/core/constants"
     9  	"gitlab.com/evatix-go/core/coredata/corejson"
    10  )
    11  
    12  type HashsetsCollection struct {
    13  	items []*Hashset
    14  }
    15  
    16  func (it *HashsetsCollection) IsEmpty() bool {
    17  	return it.Length() == 0
    18  }
    19  
    20  func (it *HashsetsCollection) HasItems() bool {
    21  	return it.Length() > 0
    22  }
    23  
    24  func (it *HashsetsCollection) IndexOf(index int) *Hashset {
    25  	if it.IsEmpty() ||
    26  		it.Length()-1 > index {
    27  		return nil
    28  	}
    29  
    30  	hashset := it.items[index]
    31  
    32  	return hashset
    33  }
    34  
    35  func (it *HashsetsCollection) ListPtr() *[]*Hashset {
    36  	return &it.items
    37  }
    38  
    39  func (it *HashsetsCollection) List() []*Hashset {
    40  	return it.items
    41  }
    42  
    43  func (it *HashsetsCollection) StringsList() *[]string {
    44  	if it.IsEmpty() {
    45  		return constants.EmptyStringsPtr
    46  	}
    47  
    48  	completeLength := 0
    49  	for _, hashset := range it.items {
    50  		completeLength += hashset.Length()
    51  	}
    52  
    53  	stringsList := make([]string, completeLength)
    54  	index := 0
    55  
    56  	for _, hashset := range it.items {
    57  		for _, item := range *hashset.ListPtr() {
    58  			stringsList[index] = item
    59  			index++
    60  		}
    61  	}
    62  
    63  	return &stringsList
    64  }
    65  
    66  // HasAll items returns false
    67  // hashsetsCollection Empty returns false
    68  func (it *HashsetsCollection) HasAll(items ...string) bool {
    69  	if it.IsEmpty() || items == nil {
    70  		return false
    71  	}
    72  
    73  	length := it.Length()
    74  	boolList := make([]bool, length)
    75  	wg := sync.WaitGroup{}
    76  	wg.Add(length)
    77  	hasFunc := func(i int) {
    78  		boolList[i] = it.items[i].
    79  			HasAllStrings(items)
    80  		wg.Done()
    81  	}
    82  
    83  	for i := 0; i < length; i++ {
    84  		go hasFunc(i)
    85  	}
    86  
    87  	wg.Wait()
    88  
    89  	for i := 0; i < length; i++ {
    90  		if boolList[i] {
    91  			return true
    92  		}
    93  	}
    94  
    95  	return false
    96  }
    97  
    98  func (it *HashsetsCollection) ListDirectPtr() *[]Hashset {
    99  	list := make([]Hashset, it.Length())
   100  
   101  	for i, hashset := range it.items {
   102  		//goland:noinspection GoLinterLocal,GoVetCopyLock
   103  		list[i] = *hashset //nolint:govet
   104  	}
   105  
   106  	return &list
   107  }
   108  
   109  func (it *HashsetsCollection) AddHashsetsCollection(
   110  	next *HashsetsCollection,
   111  ) *HashsetsCollection {
   112  	if next == nil || next.IsEmpty() {
   113  		return it
   114  	}
   115  
   116  	items := it.items
   117  
   118  	for _, nextHashset := range next.items {
   119  		items = append(items, nextHashset)
   120  	}
   121  
   122  	it.items = items
   123  
   124  	return it
   125  }
   126  
   127  func (it *HashsetsCollection) ConcatNew(
   128  	nextCollections ...*HashsetsCollection,
   129  ) *HashsetsCollection {
   130  	if nextCollections == nil || len(nextCollections) == 0 {
   131  		return New.HashsetsCollection.UsingHashsetsPointers(it.items...)
   132  	}
   133  
   134  	length := it.Length() + constants.Capacity4
   135  
   136  	for _, collection := range nextCollections {
   137  		length += collection.Length()
   138  	}
   139  
   140  	newHashsetsCollection := New.HashsetsCollection.LenCap(constants.Zero, length)
   141  	newHashsetsCollection.AddHashsetsCollection(it)
   142  
   143  	for _, collection := range nextCollections {
   144  		newHashsetsCollection.AddHashsetsCollection(collection)
   145  	}
   146  
   147  	return newHashsetsCollection
   148  }
   149  
   150  func (it *HashsetsCollection) Add(
   151  	hashset *Hashset,
   152  ) *HashsetsCollection {
   153  	it.items = append(
   154  		it.items,
   155  		hashset)
   156  
   157  	return it
   158  }
   159  
   160  func (it *HashsetsCollection) AddNonNil(
   161  	hashset *Hashset,
   162  ) *HashsetsCollection {
   163  	if hashset == nil {
   164  		return it
   165  	}
   166  
   167  	it.items = append(it.items, hashset)
   168  
   169  	return it
   170  }
   171  
   172  func (it *HashsetsCollection) AddNonEmpty(
   173  	hashset *Hashset,
   174  ) *HashsetsCollection {
   175  	if hashset.IsEmpty() {
   176  		return it
   177  	}
   178  
   179  	it.items = append(it.items, hashset)
   180  
   181  	return it
   182  }
   183  
   184  // Adds nil will be skipped
   185  func (it *HashsetsCollection) Adds(
   186  	hashsets ...*Hashset,
   187  ) *HashsetsCollection {
   188  	if hashsets == nil {
   189  		return it
   190  	}
   191  
   192  	for _, hashset := range hashsets {
   193  		if hashset.IsEmpty() {
   194  			continue
   195  		}
   196  
   197  		it.items = append(
   198  			it.items,
   199  			hashset)
   200  	}
   201  
   202  	return it
   203  }
   204  
   205  func (it *HashsetsCollection) Length() int {
   206  	if it == nil || it.items == nil {
   207  		return 0
   208  	}
   209  
   210  	return len(it.items)
   211  }
   212  
   213  func (it *HashsetsCollection) LastIndex() int {
   214  	return it.Length() - 1
   215  }
   216  
   217  func (it *HashsetsCollection) IsEqual(another HashsetsCollection) bool {
   218  	return it.IsEqualPtr(&another)
   219  }
   220  
   221  func (it *HashsetsCollection) IsEqualPtr(another *HashsetsCollection) bool {
   222  	if it == nil && another == nil {
   223  		return true
   224  	}
   225  
   226  	if it == nil || another == nil {
   227  		return false
   228  	}
   229  
   230  	if it == another {
   231  		// ptr same
   232  		return true
   233  	}
   234  
   235  	if it.IsEmpty() && another.IsEmpty() {
   236  		return true
   237  	}
   238  
   239  	if it.IsEmpty() || another.IsEmpty() {
   240  		return false
   241  	}
   242  
   243  	leftLength := it.Length()
   244  	rightLength := another.Length()
   245  
   246  	if leftLength != rightLength {
   247  		return false
   248  	}
   249  
   250  	for i, hashset := range it.items {
   251  		anotherHashset := another.items[i]
   252  
   253  		if !hashset.IsEqualsPtr(anotherHashset) {
   254  			return false
   255  		}
   256  	}
   257  
   258  	return true
   259  }
   260  
   261  func (it *HashsetsCollection) JsonModel() *HashsetsCollectionDataModel {
   262  	return NewHashsetsCollectionDataModelUsing(it)
   263  }
   264  
   265  func (it *HashsetsCollection) JsonModelAny() interface{} {
   266  	return it.JsonModel()
   267  }
   268  
   269  func (it *HashsetsCollection) MarshalJSON() ([]byte, error) {
   270  	return json.Marshal(it.JsonModel())
   271  }
   272  
   273  func (it *HashsetsCollection) UnmarshalJSON(
   274  	data []byte,
   275  ) error {
   276  	var dataModel HashsetsCollectionDataModel
   277  	err := json.Unmarshal(data, &dataModel)
   278  
   279  	if err == nil {
   280  		it.items = dataModel.Items
   281  	}
   282  
   283  	return err
   284  }
   285  
   286  func (it HashsetsCollection) Json() corejson.Result {
   287  	return corejson.New(it)
   288  }
   289  
   290  func (it HashsetsCollection) JsonPtr() *corejson.Result {
   291  	return corejson.NewPtr(it)
   292  }
   293  
   294  func (it *HashsetsCollection) ParseInjectUsingJson(
   295  	jsonResult *corejson.Result,
   296  ) (*HashsetsCollection, error) {
   297  	err := jsonResult.Unmarshal(it)
   298  
   299  	if err != nil {
   300  		return Empty.HashsetsCollection(), err
   301  	}
   302  
   303  	return it, nil
   304  }
   305  
   306  // ParseInjectUsingJsonMust Panic if error
   307  func (it *HashsetsCollection) ParseInjectUsingJsonMust(
   308  	jsonResult *corejson.Result,
   309  ) *HashsetsCollection {
   310  	hashSet, err := it.
   311  		ParseInjectUsingJson(jsonResult)
   312  
   313  	if err != nil {
   314  		panic(err)
   315  	}
   316  
   317  	return hashSet
   318  }
   319  
   320  func (it *HashsetsCollection) String() string {
   321  	if it.IsEmpty() {
   322  		return commonJoiner + NoElements
   323  	}
   324  
   325  	strList := make([]string, it.Length())
   326  
   327  	for i, hashset := range it.items {
   328  		strList[i] = hashset.String()
   329  	}
   330  
   331  	return strings.Join(
   332  		strList,
   333  		"")
   334  }
   335  
   336  func (it *HashsetsCollection) Join(
   337  	separator string,
   338  ) string {
   339  	return strings.Join(
   340  		*it.StringsList(),
   341  		separator)
   342  }
   343  
   344  func (it *HashsetsCollection) Serialize() ([]byte, error) {
   345  	return corejson.Serialize.Raw(it)
   346  }
   347  
   348  func (it *HashsetsCollection) AsJsonContractsBinder() corejson.JsonContractsBinder {
   349  	return it
   350  }
   351  
   352  func (it *HashsetsCollection) AsJsoner() corejson.Jsoner {
   353  	return it
   354  }
   355  
   356  func (it *HashsetsCollection) JsonParseSelfInject(
   357  	jsonResult *corejson.Result,
   358  ) error {
   359  	_, err := it.ParseInjectUsingJson(
   360  		jsonResult,
   361  	)
   362  
   363  	return err
   364  }
   365  
   366  func (it *HashsetsCollection) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector {
   367  	return it
   368  }
   369  
   370  func (it *HashsetsCollection) AsJsonMarshaller() corejson.JsonMarshaller {
   371  	return it
   372  }
   373  
   374  func (it HashsetsCollection) Deserialize(toPtr interface{}) (parsingErr error) {
   375  	return it.JsonPtr().Deserialize(toPtr)
   376  }