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

com.eimapi.fileserver.model.FileDescriptor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 eimapi.com
 *
 * 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 com.eimapi.fileserver.model;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.eimapi.fileserver.exception.FileServerException;

/**
 * The abstract file descriptor for all
 * {@link com.eimapi.fileserver.FileService} implementations
 *
 * @author Denys G. Santos
 * @version 0.0.1
 * @since 0.0.1
 */
public abstract class FileDescriptor implements Cloneable, Serializable {
	private static final long serialVersionUID = 3216870340555667470L;

	private String uuid;
	private String mimetype;
	private String encode;

	/**
	 * Default constructor. This constructor create itself their own uuid value,
	 * that can't be overwritten latter.
	 * 

* If you wish set up your own uuid you should use the * {@link FileDescriptor#FileDescriptor(String)} constructor. */ public FileDescriptor() { this(UUID.randomUUID().toString()); } /** * Constructor that receive by parameter an UUID. *

* If you do not need to set any specific uuid, is strongly recommended the use * of {@link FileDescriptor#FileDescriptor()} constructor. * * @param uuid * the uuid */ public FileDescriptor(String uuid) { this.setUuid(uuid); this.setDefaultPropertiesIfNull(); } /** * Constructor that use a {@link Map} to create object. *

* As this constructor uses reflection the names of keys need to be the name of * field. Case a key has no field candidate any error will be shown in the stack * trace and this error will be ignored by method * * @param properties * the map properties * @throws FileServerException if any error occur */ public FileDescriptor(Map properties) throws FileServerException { this.fromMap(properties); this.setDefaultPropertiesIfNull(); } /** * copy from map to this object * * @param properties * the map properties * @throws FileServerException * if any errors occurs */ public void fromMap(Map properties) throws FileServerException { List result = this.getAllFields(); for (Field field1 : result) { try { if(Modifier.isStatic(field1.getModifiers()) || Modifier.isFinal(field1.getModifiers())) { continue; } field1.setAccessible(true); field1.set(this, properties.get(field1.getName())); } catch (IllegalAccessException e) { throw new FileServerException(e.getMessage(), e); } } } /** * set default values if field is null. */ private void setDefaultPropertiesIfNull() { if (this.uuid == null) this.uuid = UUID.randomUUID().toString(); if (this.encode == null) this.encode = "UTF8"; if (this.mimetype == null) this.mimetype = "text/plain"; } /** * Get the file descriptor uuid * * @return UUID the file uuid */ public String getUuid() { return uuid; } /** * private set uuid method. *

* This method is only used by constructor and can't be overwritten. * * @param uuid * the uuid */ private final void setUuid(String uuid) { this.uuid = uuid; } /** * Get the file mimetype. * * @return String the mimetype in {@link String} format */ public String getMimetype() { return mimetype; } /** * Set the file mimetype. * * @param mimetype * the file mimetype */ public void setMimetype(String mimetype) { this.mimetype = mimetype; } /** * Get the file encode. * * @return String file encode in {@link String} format */ public String getEncode() { return encode; } /** * Set the file encode. * * @param encode * the file encode */ public void setEncode(String encode) { this.encode = encode; } /** * The clone method * * @return the cloned Object */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); return null;// This should never happen } } /** * Get all {@link Field} from class and subclasses * * @return List of fields */ private List getAllFields() { List result = new ArrayList<>(); Class i = this.getClass(); while (i != null && i != Object.class) { Collections.addAll(result, i.getDeclaredFields()); i = i.getSuperclass(); } return result; } /** * Transform the Object in a {@link Map} with all declared fields * * @return Map the declared fields map * @throws FileServerException * if any error occur */ public Map toMap() throws FileServerException { List result = this.getAllFields(); Map objectMap = new HashMap<>(result.size()); for (Field field : result) { if(field.getName().equals("serialVersionUID")) { continue; } field.setAccessible(true); try { objectMap.put(field.getName(), field.get(this)); } catch (IllegalAccessException e) { throw new FileServerException(e.getMessage(), e); } } return objectMap; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof FileDescriptor)) return false; FileDescriptor that = (FileDescriptor) o; if (getUuid() != null ? !getUuid().equals(that.getUuid()) : that.getUuid() != null) return false; if (getMimetype() != null ? !getMimetype().equals(that.getMimetype()) : that.getMimetype() != null) return false; if (getEncode() != null ? !getEncode().equals(that.getEncode()) : that.getEncode() != null) return false; return true; } @Override public int hashCode() { int result = getUuid() != null ? getUuid().hashCode() : 0; result = 31 * result + (getMimetype() != null ? getMimetype().hashCode() : 0); result = 31 * result + (getEncode() != null ? getEncode().hashCode() : 0); return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy