-
Notifications
You must be signed in to change notification settings - Fork 199
/
preProcessorsContainer.go
111 lines (91 loc) · 2.84 KB
/
preProcessorsContainer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package containers
import (
"github.com/ElrondNetwork/elrond-go/data/block"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/cornelk/hashmap"
)
// preProcessorsContainer is an PreProcessors holder organized by type
type preProcessorsContainer struct {
objects *hashmap.HashMap
}
// NewPreProcessorsContainer will create a new instance of a container
func NewPreProcessorsContainer() *preProcessorsContainer {
return &preProcessorsContainer{
objects: &hashmap.HashMap{},
}
}
// Get returns the object stored at a certain key.
// Returns an error if the element does not exist
func (ppc *preProcessorsContainer) Get(key block.Type) (process.PreProcessor, error) {
value, ok := ppc.objects.Get(uint8(key))
if !ok {
return nil, process.ErrInvalidContainerKey
}
preProcessor, ok := value.(process.PreProcessor)
if !ok {
return nil, process.ErrWrongTypeInContainer
}
return preProcessor, nil
}
// Add will add an object at a given key. Returns
// an error if the element already exists
func (ppc *preProcessorsContainer) Add(key block.Type, preProcessor process.PreProcessor) error {
if preProcessor == nil || preProcessor.IsInterfaceNil() {
return process.ErrNilContainerElement
}
ok := ppc.objects.Insert(uint8(key), preProcessor)
if !ok {
return process.ErrContainerKeyAlreadyExists
}
return nil
}
// AddMultiple will add objects with given keys. Returns
// an error if one element already exists, lengths mismatch or an interceptor is nil
func (ppc *preProcessorsContainer) AddMultiple(keys []block.Type, PreProcessors []process.PreProcessor) error {
if len(keys) != len(PreProcessors) {
return process.ErrLenMismatch
}
for idx, key := range keys {
err := ppc.Add(key, PreProcessors[idx])
if err != nil {
return err
}
}
return nil
}
// Replace will add (or replace if it already exists) an object at a given key
func (ppc *preProcessorsContainer) Replace(key block.Type, preProcessor process.PreProcessor) error {
if preProcessor == nil || preProcessor.IsInterfaceNil() {
return process.ErrNilContainerElement
}
ppc.objects.Set(uint8(key), preProcessor)
return nil
}
// Remove will remove an object at a given key
func (ppc *preProcessorsContainer) Remove(key block.Type) {
ppc.objects.Del(uint8(key))
}
// Len returns the length of the added objects
func (ppc *preProcessorsContainer) Len() int {
return ppc.objects.Len()
}
// Keys returns all the keys from the container
func (ppc *preProcessorsContainer) Keys() []block.Type {
keys := make([]block.Type, 0)
for key := range ppc.objects.Iter() {
uint8key, ok := key.Key.(uint8)
if !ok {
continue
}
blockType := block.Type(uint8key)
keys = append(keys, blockType)
}
return keys
}
// IsInterfaceNil returns true if there is no value under the interface
func (ppc *preProcessorsContainer) IsInterfaceNil() bool {
if ppc == nil {
return true
}
return false
}