blob: 43f2b516f5d6e8918417d3566ea8efd426b1a4c4 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.jclouds.snia.cdmi.v1.domain;
import static com.google.common.base.Objects.equal;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import com.google.common.base.Charsets;
import com.google.common.base.Objects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
public class DataObject extends CDMIObject {
public static Builder<?> builder() {
return new ConcreteBuilder();
}
@Override
public Builder<?> toBuilder() {
return builder().fromDataObject(this);
}
public static class Builder<B extends Builder<B>> extends CDMIObject.Builder<B> {
private String mimetype = "";
private String value = "";
/**
* @see DataObject#getMimetype()
*/
public B mimetype(String mimetype) {
this.mimetype = mimetype;
return self();
}
/**
* @see DataObject#getValueAsString()
*/
public B value(String value) {
this.value = value;
return self();
}
@Override
public DataObject build() {
return new DataObject(this);
}
public B fromDataObject(DataObject in) {
return fromCDMIObject(in).mimetype(in.getMimetype());
}
}
private static class ConcreteBuilder extends Builder<ConcreteBuilder> {
}
private final String mimetype;
private final String value;
protected DataObject(Builder<?> builder) {
super(builder);
this.mimetype = checkNotNull(builder.mimetype, "mimetype");
this.value = checkNotNull(builder.value, "value");
}
/**
* get dataObject's mimetype.
*/
public String getMimetype() {
return mimetype;
}
/**
* get dataObject's value as a String
*/
public String getValueAsString() {
return value;
}
/**
* get dataObject's value as a InputStream value character set is Charsets.UTF_8
*
* @return value
*/
public ByteSource getValueAsByteSource() {
return ByteSource.wrap(value.getBytes(Charsets.UTF_8));
}
/**
* get dataObject's value as a InputStream
*
* @param charset
* value character set
* @return value
*/
public ByteSource getValueAsByteSource(Charset charset) {
return ByteSource.wrap(value.getBytes(charset));
}
/**
* get dataObject's value as a ByteArray value character set is Charsets.UTF_8
*
* @return value
*/
public byte[] getValueAsByteArray() {
return value.getBytes(Charsets.UTF_8);
}
/**
* get dataObject's value as a InputStream
*
* @param charset
* value character set
* @return value
*/
public byte[] getValueAsByteArray(Charset charset) {
return value.getBytes(charset);
}
/**
* get dataObject's value as a File value character set is Charsets.UTF_8
*
* @param destDir
* destination directory
* @return value
*/
public File getValueAsFile(File destDir) throws IOException {
File fileOut = new File(destDir, this.getObjectName());
getValueAsByteSource().copyTo(Files.asByteSink(fileOut));
return fileOut;
}
/**
* get dataObject's value as a File
*
* @param charset
* value character set
* @return value
*/
public File getValueAsFile(File destDir, Charset charset) throws IOException {
File fileOut = new File(destDir, this.getObjectName());
getValueAsByteSource(charset).copyTo(Files.asByteSink(fileOut));
return fileOut;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
DataObject that = DataObject.class.cast(o);
return super.equals(that) && equal(this.mimetype, that.mimetype) && equal(this.value, that.value);
}
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), mimetype, value);
}
@Override
public ToStringHelper string() {
return super.string().add("mimetype", mimetype);
}
}