This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.io.storage.ram;
020
021import org.apache.reef.io.Accumulator;
022import org.apache.reef.io.Spool;
023
024import javax.inject.Inject;
025import java.util.ArrayList;
026import java.util.ConcurrentModificationException;
027import java.util.Iterator;
028import java.util.List;
029
030/**
031 * A SpoolFile implementation that is backed by RAM.
032 * <p>
033 * It uses an ArrayList to store the objects in.
034 */
035public final class RamSpool<T> implements Spool<T> {
036
037  private final List<T> backingStore = new ArrayList<>();
038  private boolean canAppend = true;
039  private boolean canGetAccumulator = true;
040
041  @Inject
042  public RamSpool(final RamStorageService ramStore) {
043  }
044
045  @Override
046  public Iterator<T> iterator() {
047    canAppend = false;
048    return backingStore.iterator();
049  }
050
051  @Override
052  public Accumulator<T> accumulator() {
053    if (!canGetAccumulator) {
054      throw new UnsupportedOperationException("Can only getAccumulator() once!");
055    }
056    canGetAccumulator = false;
057    return new Accumulator<T>() {
058      @Override
059      public void add(final T datum) {
060        if (!canAppend) {
061          throw new ConcurrentModificationException("Attempt to append after creating iterator!");
062        }
063        backingStore.add(datum);
064      }
065
066      @Override
067      public void close() {
068        canAppend = false;
069      }
070    };
071  }
072}