001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.schematic.document;
017
018import org.modeshape.schematic.annotation.Immutable;
019import org.modeshape.schematic.internal.document.BsonUtils;
020
021/**
022 * A {@link Bson.Type#OBJECTID ObjectId} value for use within a {@link Document BSON object}, and are 12-byte binary values
023 * designed to have a reasonably high probability of being unique when allocated.
024 * 
025 * @author Randall Hauch <rhauch@redhat.com> (C) 2011 Red Hat Inc.
026 */
027@Immutable
028public final class ObjectId {
029
030    private final int time;
031    private final int machine;
032    private final int process;
033    private final int inc;
034
035    public ObjectId( int time,
036                     int machine,
037                     int process,
038                     int inc ) {
039        this.time = time;
040        this.machine = machine;
041        this.process = process;
042        this.inc = inc;
043    }
044
045    public int getTime() {
046        return time;
047    }
048
049    public int getMachine() {
050        return machine;
051    }
052
053    public int getProcess() {
054        return process;
055    }
056
057    public int getInc() {
058        return inc;
059    }
060
061    public byte[] getBytes() {
062        byte b[] = new byte[12];
063        BsonUtils.writeObjectId(this, b);
064        return b;
065    }
066    
067    public String getBytesInBase16() {
068        byte b[] = getBytes();
069        StringBuilder buf = new StringBuilder(24);
070        for (int i = 0; i < b.length; i++) {
071            int x = b[i] & 0xFF;
072            String s = Integer.toHexString(x);
073            if (s.length() == 1) {
074                buf.append("0");
075            }
076            buf.append(s);
077        }
078        return buf.toString();
079    }
080
081    @Override
082    public int hashCode() {
083        return getTime();
084    }
085
086    @Override
087    public boolean equals( Object obj ) {
088        if (obj == this) return true;
089        if (obj instanceof ObjectId) {
090            ObjectId that = (ObjectId)obj;
091            return this.getTime() == that.getTime() && this.getMachine() == that.getMachine()
092                   && this.getProcess() == this.getProcess() && this.getInc() == that.getInc();
093        }
094        return false;
095    }
096
097    @Override
098    public String toString() {
099        return "ObjectID(" + time + ':' + machine + ':' + process + ':' + inc + ')';
100    }
101}