Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d9213a8ef47f5d73ec1f1c538a7d59ed98bc35cd (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 * 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.ui.dnd;

import java.util.Collections;
import java.util.List;

import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.util.EcoreUtil;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;


/**
 * This is the CDOResourceURITransferData type. Enjoy.
 */
public final class CDOResourceURITransferData {

	private static final String NS_URI = "http://www.eclipse.org/papyrus/1.0.0/cdo/private/dnd"; //$NON-NLS-1$

	private static final EClass ECLASS = (EClass) EPackage.Registry.INSTANCE.getEPackage(NS_URI).getEClassifier(CDOResourceURITransferData.class.getSimpleName());

	private static final EAttribute URIS = (EAttribute) ECLASS.getEStructuralFeature("resourceURIs"); //$NON-NLS-1$

	private final List<URI> uris;

	public CDOResourceURITransferData(Iterable<? extends CDOResourceNode> resourceNodes) {
		this(ImmutableList.copyOf(Iterables.transform(resourceNodes, new Function<CDOResourceNode, URI>() {

			@Override
			public URI apply(CDOResourceNode input) {
				return input.getURI();
			}
		})));
	}

	private CDOResourceURITransferData(List<URI> uris) {
		this.uris = uris;
	}

	public List<URI> getURIs() {
		return uris;
	}

	public byte[] serialize() {
		ByteArrayDataOutput data = ByteStreams.newDataOutput();

		data.writeInt(uris.size());
		for (URI next : uris) {
			data.writeUTF(next.toString());
		}

		return data.toByteArray();
	}

	public static CDOResourceURITransferData deserialize(byte[] serial) {
		ImmutableList.Builder<URI> uris = ImmutableList.builder();
		ByteArrayDataInput data = ByteStreams.newDataInput(serial);

		final int count = data.readInt();
		for (int i = 0; i < count; i++) {
			uris.add(URI.createURI(data.readUTF()));
		}

		return new CDOResourceURITransferData(uris.build());
	}

	public static boolean isCDOResourceURITransferData(EObject object) {
		return ECLASS.isInstance(object);
	}

	public static CDOResourceURITransferData fromEObject(EObject object) {
		@SuppressWarnings("unchecked")
		List<URI> uris = isCDOResourceURITransferData(object) ? (List<URI>) object.eGet(URIS) : Collections.<URI> emptyList();

		return new CDOResourceURITransferData(uris);
	}

	public EObject asEObject() {
		EObject result = EcoreUtil.create(ECLASS);
		result.eSet(URIS, getURIs());
		return result;
	}
}

Back to the top