All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.seznam.euphoria.inmem.InMemStorageProvider Maven / Gradle / Ivy

Go to download

An all-in-memory executing euphoria executor suitable for executing flows in unit tests.

The newest version!
/**
 * Copyright 2016-2017 Seznam.cz, a.s.
 *
 * 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 cz.seznam.euphoria.inmem;

import cz.seznam.euphoria.core.client.operator.state.ListStorage;
import cz.seznam.euphoria.core.client.operator.state.ListStorageDescriptor;
import cz.seznam.euphoria.core.client.operator.state.StorageProvider;
import cz.seznam.euphoria.core.client.operator.state.ValueStorage;
import cz.seznam.euphoria.core.client.operator.state.ValueStorageDescriptor;
import java.util.ArrayList;
import java.util.List;

/**
 * Provider of state storage for inmem executor.
 */
public class InMemStorageProvider implements StorageProvider {

  private static class InMemValueStateStorage implements ValueStorage {

    private final T defVal;
    T value;

    InMemValueStateStorage(T defVal) {
      this.defVal = defVal;
      this.value = defVal;
    }

    @Override
    public void set(T value) {
      this.value = value;
    }

    @Override
    public T get() {
      return value;
    }

    @Override
    public void clear() {
      this.value = defVal;
    }
  }

  private static class InMemListStateStorage implements ListStorage {

    List values = new ArrayList<>();

    @Override
    public void add(T element) {
      values.add(element);
    }

    @Override
    public Iterable get() {
      return values;
    }

    @Override
    public void clear() {
      values.clear();
    }
    
  }

  @Override
  @SuppressWarnings("unchecked")
  public  ValueStorage getValueStorage(ValueStorageDescriptor descriptor) {
    return new InMemValueStateStorage(descriptor.getDefaultValue());
  }

  @Override
  @SuppressWarnings("unchecked")
  public  ListStorage getListStorage(ListStorageDescriptor descriptor) {
    return new InMemListStateStorage();
  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy