Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c223915fb03c5b3eb87557f842d9be6dc7e61b2d (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
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.runtime.java.modelbase;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.etrice.runtime.java.messaging.IRTObject;
import org.eclipse.etrice.runtime.java.messaging.RTObject;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public abstract class ReplicatedInterfaceItemBase extends RTObject implements IReplicatedInterfaceItem, IInterfaceItemOwner {

	private int localId;
	private ArrayList<InterfaceItemBase> items = new ArrayList<InterfaceItemBase>();
	private LinkedList<Integer> releasedIndices = new LinkedList<Integer>();

	/**
	 * @param owner
	 * @param name
	 * @param localId
	 */
	protected ReplicatedInterfaceItemBase(IInterfaceItemOwner owner, String name, int localId) {
		super(owner, name);
		
		this.localId = localId;
		
		List<String> peerPaths = getParent().getPeersForPath(getInstancePath());
		if (peerPaths!=null) {
			for (String path : peerPaths) {
				IRTObject object = getObject(path);
				InterfaceItemBase peer = null;
				if (object instanceof InterfaceItemBase) {
					peer = ((InterfaceItemBase) object);
				}
				else if (object instanceof IReplicatedInterfaceItem) {
					peer = ((IReplicatedInterfaceItem) object).createSubInterfaceItem();
				}
				if (peer!=null) {
					InterfaceItemBase item = createSubInterfaceItem();
					item.connectWith(peer);
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.modelbase.IReplicatedInterfaceItem#createSubInterfaceItem()
	 */
	@Override
	public InterfaceItemBase createSubInterfaceItem() {
		InterfaceItemBase item = createInterfaceItem(this, getName()+items.size(), localId, getFreeIndex());
		items.add(item);
		return item;
	}
	
	public void removeItem(InterfaceItemBase item) {
		assert(item.getParent()==this): "is own child";
		releasedIndices.push(item.getIdx());
		items.remove(item);
	}
	
	private int getFreeIndex() {
		if (releasedIndices.isEmpty())
			return items.size();
		else
			return releasedIndices.pop();
	}
	
	public InterfaceItemBase getInterfaceItem(int idx) {
		for (InterfaceItemBase item : items) {
			if (item.getIdx()==idx)
				return item;
		}
		
		return null;
	}
	
	public int getNInterfaceItems() {
		return items.size();
	}
	
	public int getLocalId() {
		return localId;
	}
	
	@Override
	public IEventReceiver getEventReceiver() {
		return (IEventReceiver) getParent();
	}
	
	protected abstract InterfaceItemBase createInterfaceItem(IInterfaceItemOwner rcv, String name, int lid, int idx);
}

Back to the top