Package com.aragost.javahg

Source Code of com.aragost.javahg.Changeset$Extra

/*
* #%L
* JavaHg
* %%
* Copyright (C) 2011 aragost Trifork ag
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
package com.aragost.javahg;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.aragost.javahg.commands.LogCommand;
import com.aragost.javahg.internals.ExtraLogCommand;
import com.aragost.javahg.internals.HgInputStream;
import com.aragost.javahg.internals.RuntimeIOException;
import com.aragost.javahg.internals.Utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
* Represent data for a single changeset.
* <p>
* A Changeset object can be created just with the node id. The
* actually data will be loaded on demand when it is accessed
*/
public class Changeset {

    /**
     * The template use among others with the log command to read
     * changeset.
     * <p>
     * The template is parsed by the createFromInputStream() method.
     */
    public static final String CHANGESET_TEMPLATE = "\\0{node}{rev}\\n{author}\\n{date|hgdate}\\n{branch}\\n{parents}{desc}";

    /**
     * The id for the null changeset
     */
    public static final String NULL_ID = "0000000000000000000000000000000000000000";

    private final String node;
    private final Repository repository;

    /**
     * The actual data for the Changeset
     */
    protected ChangesetData data;

    /**
     * Mercurial's extra data. Lazy loaded.
     */
    private Extra extra;

    /**
     * Use {@link Repository#changeSet(String)} to create ChangeSet's
     *
     * @param repository
     */
    public Changeset(Repository repository, String node) {
        this.repository = repository;
        this.node = node;
    }

    private static Changeset createFromInputStream(Repository repository, HgInputStream in) throws IOException {
        byte[] node = in.next(40);
        if (node == null) {
            return null;
        }
        String nodeString = new String(node);
        int revision = in.revisionUpTo('\n');
        Changeset cset = repository.changeset(nodeString);
        String user = in.textUpTo('\n');
        DateTime timestamp = in.dataTimeUpTo('\n');
        String branch = in.textUpTo('\n');
        in.upTo(':');
        String p1 = new String(in.next(40));
        in.upTo(':');
        String p2 = new String(in.next(40));
        in.read(); // skip space part of {parents}
        Changeset parent1 = repository.changeset(p1);
        Changeset parent2 = repository.changeset(p2);
        String message = in.textUpTo('\0');

        ChangesetData data = cset.data;
        if (data == null) {
            data = new ChangesetData(revision, user, timestamp, branch, parent1, parent2, message);
            cset.data = data;
        }
        return cset;
    }

    public static List<Changeset> readListFromStream(Repository repository, HgInputStream in) {
        List<Changeset> changesets = Lists.newArrayList();
        try {
            in.upTo('\0');
            while (true) {
                Changeset cset = Changeset.createFromInputStream(repository, in);
                if (cset == null) {
                    break;
                }
                changesets.add(cset);
            }
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
        return changesets;
    }

    public String getNode() {
        return this.node;
    }

    public int getRevision() {
        ensureAllDataLoaded();
        return this.data.revision;
    }

    public String getUser() {
        ensureAllDataLoaded();
        return this.data.user;
    }

    public DateTime getTimestamp() {
        ensureAllDataLoaded();
        return this.data.timestamp;
    }

    public String getBranch() {
        ensureAllDataLoaded();
        return this.data.branch;
    }

    public Changeset getParent1() {
        ensureAllDataLoaded();
        return this.data.parent1;
    }

    public Changeset getParent2() {
        ensureAllDataLoaded();
        return this.data.parent2;
    }

    public String getMessage() {
        ensureAllDataLoaded();
        return this.data.message;
    }

    private void ensureAllDataLoaded() {
        if (this.data != null) {
            return;
        }
        LogCommand.on(this.repository).rev(getNode()).execute();
        if (this.data == null) {
            throw new IllegalStateException("data was not loaded");
        }
    }

    public Phase readPhase() {
        Map<Changeset, Phase> phases = getRepository().readPhases(getNode());
        return phases.get(this);
    }

    /**
     *
     * @return Mercurial's extra dictionary
     */
    public synchronized Extra getExtra() {
        if (this.extra == null) {
            ExtraLogCommand cmd = new ExtraLogCommand(getRepository());
            cmd.rev(getNode());
            this.extra = new Extra(cmd.execute());
        }
        return this.extra;
    }

    @Override
    public boolean equals(Object that) {
        if (that instanceof Changeset) {
            return equals((Changeset) that);
        } else {
            return false;
        }
    }

    public boolean equals(Changeset that) {
        if (this.repository != that.repository) {
            return false;
        }
        return this.getNode().equals(that.getNode());
    }

    @Override
    public int hashCode() {
        return getNode().hashCode();
    }

    @Override
    public String toString() {
        return "changeset[" + this.node + "]";
    }

    @VisibleForTesting
    ChangesetData getData() {
        return this.data;
    }

    Repository getRepository() {
        return repository;
    }

    private String decodeBytes(byte[] bytes) {
        return Utils.decodeBytes(bytes, Changeset.this.getRepository().getServer().getDecoder());
    }

    /**
     * Class representing the extra dictionary Mercurial has for each
     * changeset.
     * <p>
     * The values can be binary data, but is typically strings. For
     * this reason there is accessor methods to access the values as
     * both byte array and String.
     */
    public class Extra {

        private final Map<String, byte[]> map;

        private Extra(Map<String, byte[]> map) {
            this.map = map;
        }

        /**
         * @param key
         * @return The extra data for the key as a String
         */
        public String getString(String key) {
            byte[] bytes = getBytes(key);
            if (bytes == null) {
                return null;
            } else {
                return decodeBytes(bytes);
            }
        }

        /**
         * @param key
         * @return The extra data for the key as byte array
         */
        public byte[] getBytes(String key) {
            return map.get(key);
        }

        /**
         * @return a view on the extra data dictionary where values
         *         are Strings.
         */
        public Map<String, String> stringValuedMap() {
            Function<byte[], String> f = new Function<byte[], String>() {
                public String apply(byte[] input) {
                    return decodeBytes(input);
                }
            };
            return Maps.transformValues(this.map, f);
        }

        /**
         * @return a view on the extra data dictionary where values
         *         are byte arrays.
         */
        public Map<String, byte[]> byteArrayValuedMap() {
            return this.map;
        }

    }

}

class ChangesetData {
    public int revision;
    public String user;
    public DateTime timestamp;
    public String branch;
    public Changeset parent1;
    public Changeset parent2;
    public String message;

    public ChangesetData(int revision, String user, DateTime timestamp, String branch, Changeset parent1,
            Changeset parent2, String message) {
        this.revision = revision;
        this.user = user;
        this.timestamp = timestamp;
        this.branch = branch;
        this.parent1 = parent1;
        this.parent2 = parent2;
        this.message = message;
    }
}
TOP

Related Classes of com.aragost.javahg.Changeset$Extra

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.