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

br.com.objectos.way.gdrive.ChangeId 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.io.File;
import java.io.IOException;

import com.google.api.client.repackaged.com.google.common.base.Objects;
import com.google.api.client.repackaged.com.google.common.base.Throwables;
import com.google.api.services.drive.Drive.Changes;
import com.google.common.base.Charsets;
import com.google.common.io.Files;

/**
 * @author [email protected] (Mario Marques Junior)
 */
public abstract class ChangeId {

  ChangeId() {
  }

  abstract Long getId();

  abstract boolean matches(GChange change);

  abstract void setStartChangeIdOf(Changes.List _request);

  abstract File write(File file);

  static ChangeId empty() {
    return EmptyChangeId.INSTANCE;
  }

  static ChangeId of(Long id) {
    return new ValidChangeId(id);
  }

  static ChangeId of(File file) {
    try {
      String string = Files.toString(file, Charsets.UTF_8);
      Long id = Long.parseLong(string);

      return new ValidChangeId(id);
    } catch (Exception e) {
      return EmptyChangeId.INSTANCE;
    }
  }

  static class EmptyChangeId extends ChangeId {

    static final ChangeId INSTANCE = new EmptyChangeId();

    private EmptyChangeId() {
    }

    @Override
    public boolean matches(GChange change) {
      return false;
    }

    @Override
    public void setStartChangeIdOf(Changes.List _request) {
    }

    @Override
    Long getId() {
      return Long.MIN_VALUE;
    }

    @Override
    File write(File file) {
      return file;
    }

  }

  static class ValidChangeId extends ChangeId {

    private final Long id;

    ValidChangeId(Long id) {
      this.id = id;
    }

    @Override
    public boolean matches(GChange change) {
      ChangeId that = change.getId();
      return Objects.equal(this.getId(), that.getId());
    }

    @Override
    public void setStartChangeIdOf(Changes.List _request) {
      _request.setStartChangeId(id);
    }

    @Override
    Long getId() {
      return id;
    }

    @Override
    File write(File to) {
      try {
        String from = Long.toString(id);
        Files.write(from, to, Charsets.UTF_8);

        return to;
      } catch (IOException e) {
        throw Throwables.propagate(e);
      }
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy