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

com.sappenin.utils.rest.v2.model.impl.AbstractRestModel Maven / Gradle / Ivy

Go to download

Utilities, Helpers, and base-classes for assisting in server-side REST API implementations written in Java.

The newest version!
/**
 * Copyright (C) 2014 Sappenin Inc. ([email protected])
 * 
 * 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.sappenin.utils.rest.v2.model.impl;

import java.util.Map;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.sappenin.utils.rest.v2.InternalId;
import com.sappenin.utils.rest.v2.model.RestModel;
import com.sappenin.utils.rest.v2.model.meta.Meta;

/**
 * An abstract implementation of {@link RestModel}.
 */
@Getter
@EqualsAndHashCode
@ToString
@JsonPropertyOrder({
	"meta", "optId", "additionalAttributes"
})
public abstract class AbstractRestModel implements RestModel
{
	private static final long serialVersionUID = 6523915199962689298L;

	@JsonProperty("meta")
	private final Optional optMeta;

	// An internal identifier for compartmentalized use by server-side or client-side applications.
	@JsonProperty("id")
	private final Optional optId;

	@Getter
	@JsonProperty("additionalAttributes")
	@JsonUnwrapped
	@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", timezone = "UTC")
	private Optional> optAdditionalAttributes = Optional.absent();

	/**
	 * Required Args Constructor.
	 *
	 * @param optMeta An optionally present instance of .
	 * @param optId An optionally present instance of {@link InternalId} that represents the server-side internal
	 *            identifier of a resource. This can be used to represent something like an integer, long number or
	 *            other type as a String identifier that both client and server can use to aid in locating the actual
	 *            resource.
	 * @param optAdditionalAttributes An optionally present instance of {@link Map} that holds additional attributes for
	 *            this resource representation.
	 */
	public AbstractRestModel(@JsonProperty("meta") final Optional optMeta,
			@JsonProperty("id") final Optional optId,
			@JsonProperty("additionalAttributes") final Optional> optAdditionalAttributes)
	{
		// This can occasionally be null, such as when deserializing Json from a client update request.
		this.optMeta = Preconditions.checkNotNull(optMeta);
		this.optId = Preconditions.checkNotNull(optId);
		this.optAdditionalAttributes = Preconditions.checkNotNull(optAdditionalAttributes);
	}

	/**
	 * A copy-constructor of sorts that initializes this instance from the supplied {@link RestModel}.
	 * 
	 * @param restModel An instance of {@link RestModel} to copy from.
	 */
	public AbstractRestModel(final RestModel restModel)
	{
		Preconditions.checkNotNull(restModel);

		this.optMeta = restModel.getOptMeta();
		this.optId = restModel.getOptId();
		this.optAdditionalAttributes = restModel.getOptAdditionalAttributes();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy