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

br.com.objectos.way.gdrive.GObserverPojo Maven / Gradle / Ivy

The newest version!
/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.way.gdrive;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.google.api.services.drive.model.Change;
import com.google.api.services.drive.model.File;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;

/**
 * @author [email protected] (Marcio Endo)
 */
class GObserverPojo implements GObserver {

  private final AtomicReference changeIdStore;

  private final DriveExec exec;
  private final File file;

  public GObserverPojo(DriveExec exec, File file) {
    this.exec = exec;
    this.file = file;

    changeIdStore = new AtomicReference(ChangeId.empty());
  }

  @Override
  public List listChanges() {
    ChangeId changeId;
    changeId = changeIdStore.get();

    Iterator iterator;
    iterator = exec.iterateChangesOf(file, changeId);

    Iterator _changes;
    _changes = Iterators.transform(iterator, new ToGChange());

    Iterator filtered;
    filtered = Iterators.filter(_changes, Predicates.not(new IsSelf(changeId)));

    List changes;
    changes = ImmutableList.copyOf(filtered);

    updateChangeId(changes);

    return changes;
  }

  @Override
  public void resetTo(ChangeId changeId) {
    changeIdStore.set(changeId);
  }

  private void updateChangeId(List changes) {
    int size = changes.size();
    if (size > 0) {
      GChange last = changes.get(size - 1);
      ChangeId lastChangeId = last.getId();
      changeIdStore.set(lastChangeId);
    }
  }

  private class IsSelf implements Predicate {

    private final ChangeId changeId;

    public IsSelf(ChangeId changeId) {
      this.changeId = changeId;
    }

    @Override
    public boolean apply(GChange input) {
      return changeId.matches(input);
    }

  }

  private class ToGChange implements Function {
    @Override
    public GChange apply(Change input) {
      return new GChangePojo(exec, input);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy