/*
* 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.InputStream;
import java.util.Iterator;
import java.util.List;
import com.google.api.services.drive.model.File;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
/**
* @author mario.marques@objectos.com.br (Mario Marques Junior)
*/
class GDirectoryFolder implements GDirectory {
private final DriveExec exec;
private final File file;
private From from;
private String title;
public GDirectoryFolder(DriveExec exec, File file) {
this.exec = exec;
this.file = file;
}
@Override
public String getId() {
return file.getId();
}
@Override
public String getName() {
return file.getTitle();
}
@Override
public List<GFile> listFiles() {
Iterator<File> iterator;
iterator = exec.iterateFilesOf(file);
Iterator<GFile> files;
files = Iterators.transform(iterator, new ToGFile(exec));
return ImmutableList.copyOf(files);
}
@Override
public GDirectory mkdir(String name) {
File dir = exec.mkdir(file, name);
return new GDirectoryFolder(exec, dir);
}
@Override
public GDirectory write(java.io.File from) {
this.from = From.of(from);
this.title = from.getName();
return this;
}
@Override
public GDirectory writeStream(InputStream from, String title) {
this.from = From.of(from);
this.title = title;
return this;
}
@Override
public GFile as(MimeType mimeType) {
File res = exec.write(file, from, title, mimeType);
return new GFileBuilder(exec, res).build();
}
@Override
public GObserver observe() {
return new GObserverPojo(exec, file);
}
}