1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.Branch = function(displayName, revisions, locked)
27{
28    WebInspector.Object.call(this);
29
30    console.assert(displayName);
31
32    this._displayName = displayName;
33    this._revisions = revisions instanceof Array ? revisions.slice() : [];
34    this._locked = locked || false;
35};
36
37WebInspector.Branch.prototype = {
38    constructor: WebInspector.Branch,
39
40    // Public
41
42    get displayName()
43    {
44        return this._displayName;
45    },
46
47    set displayName(displayName)
48    {
49        console.assert(displayName);
50        if (!displayName)
51            return;
52
53        this._displayName = displayName;
54    },
55
56    get revisions()
57    {
58        return this._revisions;
59    },
60
61    get locked()
62    {
63        return this._locked;
64    },
65
66    revisionForRepresentedObject: function(representedObject, doNotCreateIfNeeded)
67    {
68        for (var i = 0; i < this._revisions.length; ++i) {
69            var revision = this._revisions[i];
70            if (revision instanceof WebInspector.SourceCodeRevision && revision.sourceCode === representedObject)
71                return revision;
72        }
73
74        if (doNotCreateIfNeeded)
75            return null;
76
77        if (representedObject instanceof WebInspector.SourceCode) {
78            var revision = representedObject.originalRevision.copy();
79            representedObject.currentRevision = revision;
80            this.addRevision(revision);
81            return revision;
82        }
83
84        return null;
85    },
86
87    addRevision: function(revision)
88    {
89        console.assert(revision instanceof WebInspector.Revision);
90
91        if (this._locked)
92            return;
93
94        if (this._revisions.contains(revision))
95            return;
96
97        this._revisions.push(revision);
98    },
99
100    removeRevision: function(revision)
101    {
102        console.assert(revision instanceof WebInspector.Revision);
103
104        if (this._locked)
105            return;
106
107        this._revisions.remove(revision);
108    },
109
110    reset: function()
111    {
112        if (this._locked)
113            return;
114
115        this._revisions = [];
116    },
117
118    fork: function(displayName)
119    {
120        var copiedRevisions = this._revisions.map(function(revision) { return revision.copy(); });
121        return new WebInspector.Branch(displayName, copiedRevisions);
122    },
123
124    apply: function()
125    {
126        for (var i = 0; i < this._revisions.length; ++i)
127            this._revisions[i].apply();
128    },
129
130    revert: function()
131    {
132        for (var i = this._revisions.length - 1; i >= 0; --i)
133            this._revisions[i].revert();
134    },
135
136    lock: function()
137    {
138        console.assert(!this._locked);
139        this._locked = true;
140    },
141
142    unlock: function()
143    {
144        console.assert(this._locked);
145        this._locked = false;
146    }
147};
148
149WebInspector.Branch.prototype.__proto__ = WebInspector.Object.prototype;
150