-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bilyana Gospodinova <[email protected]>
- Loading branch information
1 parent
dda0c80
commit 7b1d58d
Showing
4 changed files
with
323 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/utils/MapReadableKVState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.mirror.web3.state.utils; | ||
|
||
import com.swirlds.state.spi.ReadableKVState; | ||
import com.swirlds.state.spi.ReadableKVStateBase; | ||
import jakarta.annotation.Nonnull; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A simple implementation of {@link ReadableKVState} backed by a | ||
* {@link Map}. Test code has the option of creating an instance disregarding the backing map, or by | ||
* supplying the backing map to use. This latter option is useful if you want to use Mockito to spy | ||
* on it, or if you want to pre-populate it, or use Mockito to make the map throw an exception in | ||
* some strange case, or in some other way work with the backing map directly. | ||
* | ||
* @param <K> The key type | ||
* @param <V> The value type | ||
*/ | ||
public class MapReadableKVState<K, V> extends ReadableKVStateBase<K, V> { | ||
/** Represents the backing storage for this state */ | ||
private final Map<K, V> backingStore; | ||
|
||
/** | ||
* Create an instance using the given map as the backing store. This is useful when you want to | ||
* pre-populate the map, or if you want to use Mockito to mock it or cause it to throw | ||
* exceptions when certain keys are accessed, etc. | ||
* | ||
* @param stateKey The state key for this state | ||
* @param backingStore The backing store to use | ||
*/ | ||
public MapReadableKVState(@Nonnull final String stateKey, @Nonnull final Map<K, V> backingStore) { | ||
super(stateKey); | ||
this.backingStore = Objects.requireNonNull(backingStore); | ||
} | ||
|
||
@Override | ||
protected V readFromDataSource(@Nonnull K key) { | ||
return backingStore.get(key); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
protected Iterator<K> iterateFromDataSource() { | ||
return backingStore.keySet().iterator(); | ||
} | ||
|
||
@Override | ||
public long size() { | ||
return backingStore.size(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
MapReadableKVState<?, ?> that = (MapReadableKVState<?, ?>) o; | ||
return Objects.equals(getStateKey(), that.getStateKey()) && Objects.equals(backingStore, that.backingStore); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getStateKey(), backingStore); | ||
} | ||
} |
Oops, something went wrong.