Skip to main content
summaryrefslogtreecommitdiffstats
blob: e8429c4594dd7fe98d8291229b7250d8fdc910b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*******************************************************************************
 * Copyright (c) 2006, 2009 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.examples.addressbook.addressbook.spec;

import java.util.Iterator;

import org.eclipse.emf.compare.examples.addressbook.addressbook.AddressBook;
import org.eclipse.emf.compare.examples.addressbook.addressbook.AddressbookFactory;
import org.eclipse.emf.compare.examples.addressbook.addressbook.BookVersion;
import org.eclipse.emf.compare.examples.addressbook.addressbook.impl.RepositoryImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * @author Cedric Brun <cedric.brun@obeo.fr>
 */
public class RepositorySpec extends RepositoryImpl {

	@Override
	public AddressBook checkout(int id) {
		if (id == -1)
			return checkout(getHead());
		final Iterator<BookVersion> it = getHistory().iterator();
		while (it.hasNext()) {
			final BookVersion cur = it.next();
			if (cur.getId() == id)
				return checkout(cur.getBook());
		}
		return null;
	}

	private AddressBook checkout(AddressBook book) {
		return (AddressBook)EcoreUtil.copy(book);
	}

	@Override
	public void checkin() {
		final int maxversion = getMaxVersion();
		final BookVersion newVersion = AddressbookFactory.eINSTANCE.createBookVersion();
		newVersion.setId(maxversion + 1);
		newVersion.setBook((AddressBook)EcoreUtil.copy(getHead()));
	}

	private int getMaxVersion() {
		int max = -1;
		final Iterator<BookVersion> it = getHistory().iterator();
		while (it.hasNext()) {
			final BookVersion version = it.next();
			if (version.getId() > max) {
				max = version.getId();
			}
		}
		return max;
	}

}

Back to the top