org.apache.flink.runtime.state.UserFacingMapState Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.flink.runtime.state;
import org.apache.flink.api.common.state.MapState;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
/**
* Simple wrapper map state that exposes empty state properly as an empty map.
*
* @param The type of keys in the map state.
* @param The type of values in the map state.
*/
class UserFacingMapState implements MapState {
private final MapState originalState;
private final Map emptyState = Collections.emptyMap();
UserFacingMapState(MapState originalState) {
this.originalState = originalState;
}
// ------------------------------------------------------------------------
@Override
public V get(K key) throws Exception {
return originalState.get(key);
}
@Override
public void put(K key, V value) throws Exception {
originalState.put(key, value);
}
@Override
public void putAll(Map value) throws Exception {
originalState.putAll(value);
}
@Override
public void clear() {
originalState.clear();
}
@Override
public void remove(K key) throws Exception {
originalState.remove(key);
}
@Override
public boolean contains(K key) throws Exception {
return originalState.contains(key);
}
@Override
public Iterable> entries() throws Exception {
Iterable> original = originalState.entries();
return original != null ? original : emptyState.entrySet();
}
@Override
public Iterable keys() throws Exception {
Iterable original = originalState.keys();
return original != null ? original : emptyState.keySet();
}
@Override
public Iterable values() throws Exception {
Iterable original = originalState.values();
return original != null ? original : emptyState.values();
}
@Override
public Iterator> iterator() throws Exception {
Iterator> original = originalState.iterator();
return original != null ? original : emptyState.entrySet().iterator();
}
@Override
public boolean isEmpty() throws Exception {
return originalState.isEmpty();
}
}