001/**
002 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
003 *   This file is part of the LDP4j Project:
004 *     http://www.ldp4j.org/
005 *
006 *   Center for Open Middleware
007 *     http://www.centeropenmiddleware.com/
008 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
009 *   Copyright (C) 2014-2016 Center for Open Middleware.
010 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
011 *   Licensed under the Apache License, Version 2.0 (the "License");
012 *   you may not use this file except in compliance with the License.
013 *   You may obtain a copy of the License at
014 *
015 *             http://www.apache.org/licenses/LICENSE-2.0
016 *
017 *   Unless required by applicable law or agreed to in writing, software
018 *   distributed under the License is distributed on an "AS IS" BASIS,
019 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 *   See the License for the specific language governing permissions and
021 *   limitations under the License.
022 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
023 *   Artifact    : org.ldp4j.framework:ldp4j-application-kernel-api:0.2.1
024 *   Bundle      : ldp4j-application-kernel-api-0.2.1.jar
025 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
026 */
027package org.ldp4j.application.kernel.resource;
028
029import static com.google.common.base.Preconditions.checkNotNull;
030
031import java.io.Serializable;
032
033import org.ldp4j.application.data.Name;
034import org.ldp4j.application.kernel.template.ResourceTemplate;
035
036import com.google.common.base.MoreObjects;
037import com.google.common.base.Objects;
038
039public final class ResourceId implements Serializable {
040
041  private static final long serialVersionUID = -8083258917826432416L;
042
043  /**
044   * Not final to enable its usage in JPA
045   */
046  private String templateId;
047
048  /**
049   * Not final to enable its usage in JPA
050   */
051  private Name<?> name;
052
053  private ResourceId() {
054    // JPA FRIENDLY
055  }
056
057  private ResourceId(Name<?> name, String templateId) {
058    this.name = name;
059    this.templateId = templateId;
060  }
061
062  public Name<?> name() {
063    return this.name;
064  }
065
066  public String templateId() {
067    return this.templateId;
068  }
069
070  @Override
071  public int hashCode() {
072    return Objects.hashCode(this.name,this.templateId);
073  }
074
075  @Override
076  public boolean equals(Object obj) {
077    boolean result=false;
078    if(obj!=null && obj.getClass()==this.getClass()) {
079      ResourceId that=(ResourceId)obj;
080      result=
081        Objects.equal(this.name,that.name) &&
082        Objects.equal(this.templateId,that.templateId);
083    }
084    return result;
085  }
086
087  @Override
088  public String toString() {
089    return
090      MoreObjects.
091        toStringHelper(getClass()).
092          add("name", this.name).
093          add("templateId", this.templateId).
094          toString();
095  }
096
097  public static ResourceId createId(Name<?> name, ResourceTemplate template) {
098    checkNotNull(name,"Resource name cannot be null");
099    checkNotNull(template,"Template cannot be null");
100    return createId(name,template.id());
101  }
102
103  public static ResourceId createId(Name<?> name, String templateId) {
104    checkNotNull(name,"Resource name cannot be null");
105    checkNotNull(templateId,"Template identifier cannot be null");
106    return new ResourceId(name,templateId);
107  }
108
109}