/*
* 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 static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;
import java.util.List;
import br.com.objectos.comuns.base.Builder;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.ParentReference;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class FileBuilder implements Builder<File> {
private String title;
private MimeType mimeType;
private final List<String> parents = newArrayList();
@Override
public File build() {
File file = new File();
if (title != null) {
file.setTitle(title);
}
if (mimeType != null) {
mimeType.set(file);
}
if (!parents.isEmpty()) {
List<ParentReference> _parents = transform(parents, ToParentReference.INSTANCE);
List<ParentReference> parents = ImmutableList.copyOf(_parents);
file.setParents(parents);
}
return file;
}
public FileBuilder title(java.io.File file) {
String title = file.getName();
return title(title);
}
public FileBuilder title(String title) {
this.title = title;
return this;
}
public FileBuilder mimeType(MimeType mimeType) {
this.mimeType = mimeType;
return this;
}
public FileBuilder parent(String id) {
this.parents.add(id);
return this;
}
private enum ToParentReference implements Function<String, ParentReference> {
INSTANCE;
@Override
public ParentReference apply(String id) {
ParentReference parent = new ParentReference();
parent.setId(id);
return parent;
}
}
}