001package co.codewizards.cloudstore.ls.core.invoke;
002
003import static java.util.Objects.*;
004
005import java.io.Serializable;
006
007import co.codewizards.cloudstore.core.Uid;
008
009public class ObjectRef implements Serializable {
010
011        private static final long serialVersionUID = 1L;
012
013        /**
014         * Virtual method indicating that the associated {@link ObjectRef} is used by a reference on the other side.
015         * <p>
016         * This method is "virtual", because it does not exist and actually has a name that is illegal as a Java method name
017         * (thus making sure, we'll never have a collision with a real method).
018         * This special name is transferred as method-name in the remote-invocation-protocol whenever a proxy on the other side
019         * is created. Normally, there should be only one proxy, but there might be multiple, if the old objects were not yet
020         * garbage-collected. Additionally, there might be 0 for a short while - the actual removal is deferred a few seconds.
021         */
022        public static final String VIRTUAL_METHOD_NAME_INC_REF_COUNT = "*objectRef_incRefCount*";
023
024        /**
025         * Virtual method indicating that the associated {@link ObjectRef} is not needed anymore - and should
026         * eventually (when the counter is 0) be removed from the {@link ObjectManager}.
027         * <p>
028         * This method is "virtual", because it does not exist and actually has a name that is illegal as a Java method name
029         * (thus making sure, we'll never have a collision with a real method).
030         * This special name is transferred as method-name in the remote-invocation-protocol whenever a proxy on the other side
031         * is garbage-collected and the corresponding ObjectRef-to-Object-mapping should thus be removed from the
032         * {@link ObjectManager}.
033         */
034        public static final String VIRTUAL_METHOD_NAME_DEC_REF_COUNT = "*objectRef_decRefCount*";
035
036        public static final String VIRTUAL_METHOD_CLOSE_OBJECT_MANAGER = "*objectManager_close*";
037
038        private final Uid clientId;
039        private final int classId;
040        private final long objectId;
041
042        private ClassInfo classInfo;
043
044        public ObjectRef(final Uid clientId, final int classId, final long objectId) {
045                this.clientId = requireNonNull(clientId, "clientId");
046                this.classId = classId;
047                this.objectId = objectId;
048        }
049
050        public Uid getClientId() {
051                return clientId;
052        }
053        public int getClassId() {
054                return classId;
055        }
056        public long getObjectId() {
057                return objectId;
058        }
059
060        public ClassInfo getClassInfo() {
061                return classInfo;
062        }
063        public void setClassInfo(ClassInfo classInfo) {
064                this.classInfo = classInfo;
065        }
066
067        @Override
068        public int hashCode() {
069                final int prime = 31;
070                int result = 1;
071                result = prime * result + clientId.hashCode();
072                result = prime * result + (int) (objectId ^ (objectId >>> 32));
073                return result;
074        }
075
076        @Override
077        public boolean equals(final Object obj) {
078                if (this == obj)
079                        return true;
080                if (obj == null)
081                        return false;
082                if (getClass() != obj.getClass())
083                        return false;
084                final ObjectRef other = (ObjectRef) obj;
085                return this.objectId == other.objectId && this.clientId.equals(other.clientId);
086        }
087
088        @Override
089        public String toString() {
090                return String.format("%s[%s, %s, %s]", this.getClass().getSimpleName(), clientId, classId, objectId);
091        }
092}