001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.metamodel.adapter.oid;
021
022import org.hamcrest.Description;
023import org.hamcrest.Matcher;
024import org.hamcrest.TypeSafeMatcher;
025
026import org.apache.isis.applib.annotation.Value;
027import org.apache.isis.core.commons.encoding.Encodable;
028import org.apache.isis.core.metamodel.adapter.version.Version;
029
030
031/**
032 * An immutable identifier for either a root object (subtype {@link RootOid}) or 
033 * an aggregated object (subtype {@link AggregatedOid}).
034 * 
035 * <p>
036 * Note that value objects (strings, ints, {@link Value}s etc) do not have an {@link Oid}. 
037 */
038public interface Oid extends Encodable {
039
040    /**
041     * A string representation of this {@link Oid}.
042     */
043    String enString(OidMarshaller oidMarshaller);
044
045    String enStringNoVersion(OidMarshaller oidMarshaller);
046    
047    Version getVersion();
048    void setVersion(Version version);
049
050    /**
051     * Flags whether this OID is for a transient (not-yet-persisted) object, 
052     * or a view model object, or for a persistent object.
053     * 
054     * <p>
055     * In the case of an {@link AggregatedOid}, is determined by the state 
056     * of its {@link AggregatedOid#getParentOid() parent}'s {@link RootOid#isTransient() state}.
057     */
058    boolean isTransient();
059
060    boolean isViewModel();
061    
062    boolean isPersistent();
063
064    public static enum State {
065        PERSISTENT("P"), 
066        TRANSIENT("T"),
067        VIEWMODEL("V");
068        
069        private final String code;
070        private State(final String code) {
071            this.code = code;
072        }
073        
074        public boolean isTransient() {
075            return this == TRANSIENT;
076        }
077        public boolean isViewModel() {
078            return this == VIEWMODEL;
079        }
080        public boolean isPersistent() {
081            return this == PERSISTENT;
082        }
083
084        public String getCode() {
085            return code;
086        }
087    }
088
089
090    public final static class Matchers {
091
092        private Matchers() {
093        }
094
095        public static Matcher<Oid> isTransient() {
096            return new TypeSafeMatcher<Oid>() {
097
098                @Override
099                public boolean matchesSafely(final Oid item) {
100                    return item.isTransient();
101                }
102
103                @Override
104                public void describeTo(final Description description) {
105                    description.appendText("is transient");
106                }
107            };
108        }
109
110        public static Matcher<Oid> isPersistent() {
111            return new TypeSafeMatcher<Oid>() {
112
113                @Override
114                public boolean matchesSafely(final Oid item) {
115                    return !item.isTransient();
116                }
117
118                @Override
119                public void describeTo(final Description description) {
120                    description.appendText("is persistent");
121                }
122            };
123        }
124
125    }
126
127    
128}