<commit_before>/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kotcrab.vis.runtime.system;

import com.artemis.*;
import com.artemis.EntitySubscription.SubscriptionListener;
import com.artemis.annotations.Wire;
import com.artemis.utils.ImmutableBag;
import com.badlogic.gdx.utils.ObjectMap;
import com.kotcrab.vis.runtime.component.IDComponent;

/**
 * Allows to get entities by their string id that was set in VisEditor
 * @author Kotcrab
 */
@Wire
public class VisIDManager extends Manager {
	private ComponentMapper<IDComponent> idCm;
	private AspectSubscriptionManager subscriptionManager;

	private ObjectMap<String, Entity> idStore = new ObjectMap<String, Entity>();

	@Override
	protected void initialize () {
		EntitySubscription subscription = subscriptionManager.get(Aspect.all(IDComponent.class));

		subscription.addSubscriptionListener(new SubscriptionListener() {
			@Override
			public void inserted (ImmutableBag<Entity> entities) {
				ObjectMap<String, Entity> tmpCache = new ObjectMap<String, Entity>();

				for (Entity entity : entities) {
					tmpCache.put(idCm.get(entity).id, entity);
				}

				idStore.putAll(tmpCache);
			}

			@Override
			public void removed (ImmutableBag<Entity> entities) {
				for (Entity entity : entities) {
					idStore.remove(idCm.get(entity).id);
				}
			}
		});
	}

	public Entity get (String id) {
		return idStore.get(id);
	}
}
<commit_msg>Support multiple entities with same id<commit_after>/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kotcrab.vis.runtime.system;

import com.artemis.*;
import com.artemis.EntitySubscription.SubscriptionListener;
import com.artemis.annotations.Wire;
import com.artemis.utils.ImmutableBag;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import com.kotcrab.vis.runtime.component.IDComponent;

/**
 * Allows to get entities by their string id that was set in VisEditor
 * @author Kotcrab
 */
@Wire
public class VisIDManager extends Manager {
	private ComponentMapper<IDComponent> idCm;
	private AspectSubscriptionManager subscriptionManager;

	private ObjectMap<String, Array<Entity>> idStore = new ObjectMap<String, Array<Entity>>();

	@Override
	protected void initialize () {
		EntitySubscription subscription = subscriptionManager.get(Aspect.all(IDComponent.class));

		subscription.addSubscriptionListener(new SubscriptionListener() {
			@Override
			public void inserted (ImmutableBag<Entity> entities) {
				for (Entity entity : entities) {
					String id = idCm.get(entity).id;

					Array<Entity> idList = idStore.get(id);

					if (idList == null) {
						idList = new Array<Entity>();
						idStore.put(id, idList);
					}

					idList.add(entity);
				}
			}

			@Override
			public void removed (ImmutableBag<Entity> entities) {
				for (Entity entity : entities) {
					String id = idCm.get(entity).id;

					Array<Entity> idList = idStore.get(id);
					idList.removeValue(entity, true);

					if (idList.size == 0) {
						idStore.remove(id);
					}
				}
			}
		});
	}

	/**
	 * Returns entity for given ID. If multiple entities has the same id only the first one will be returned.
	 * @see #getMultiple(String)
	 */
	public Entity get (String id) {
		return getMultiple(id).get(0);
	}

	/**
	 * Returns all entities with this ID.
	 * @see #get(String)
	 */
	public Array<Entity> getMultiple (String id) {
		Array<Entity> entities = idStore.get(id);
		if (entities == null) throw new IllegalStateException("Could not find any entity with ID: " + id);
		return entities;
	}
}

{'fraction_non_alphanumeric': 0.10088191008819102, 'fraction_numerical': 0.005592600559260056, 'mean_word_length': 4.016181229773463, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6985494', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1326, 'n_words': 432}