<commit_before>/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.snowowl.snomed.datastore.id.memory;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Set;

import com.b2international.commons.VerhoeffCheck;
import com.b2international.snowowl.core.terminology.ComponentCategory;
import com.b2international.snowowl.snomed.datastore.SnomedTerminologyBrowser;
import com.b2international.snowowl.snomed.datastore.id.AbstractSnomedIdentifierServiceImpl;
import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifier;
import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifiers;
import com.b2international.snowowl.snomed.datastore.id.gen.ItemIdGenerationStrategy;
import com.b2international.snowowl.snomed.datastore.id.reservations.ISnomedIdentiferReservationService;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.inject.Provider;

/**
 * In memory implementation of the identifier service.
 * 
 * @since 4.5
 */
public class InMemorySnomedIdentifierServiceImpl extends AbstractSnomedIdentifierServiceImpl {

	private ISnomedIdentiferReservationService reservationService;
	private ItemIdGenerationStrategy generationStrategy;

	// TODO update that this contains only IDs during transactions
	private Set<String> reservedComponentIds = Sets.newHashSet();

	public InMemorySnomedIdentifierServiceImpl(final ItemIdGenerationStrategy generationStrategy,
			final Provider<SnomedTerminologyBrowser> provider) {
		super(provider);
		this.generationStrategy = generationStrategy;
	}

	@Override
	public boolean includes(final SnomedIdentifier identifier) {
		return super.includes(identifier) || reservedComponentIds.contains(identifier.toString());
	}

	@Override
	public SnomedIdentifier generate(final String namespace, final ComponentCategory category) {
		checkNotNull(category, "Component category must not be null.");
		checkCategory(category);

		final String componentId = generateId(namespace, category);
		reservedComponentIds.add(componentId);

		return SnomedIdentifiers.of(componentId);
	}

	@Override
	public void register(final SnomedIdentifier identifier) {
		if (reservationService.isReserved(identifier.toString())) {
			// TODO change exception
			throw new RuntimeException("Component ID is already registered.");
		} else {
			reservedComponentIds.add(identifier.toString());
		}
	}

	@Override
	public SnomedIdentifier reserve(final String namespace, final ComponentCategory category) {
		checkNotNull(category, "Component category must not be null.");
		checkCategory(category);

		final String componentId = generateId(namespace, category);
		reservedComponentIds.add(componentId);

		return SnomedIdentifiers.of(componentId);
	}

	@Override
	public void deprecate(final SnomedIdentifier identifier) {
		// TODO what to do in memory implementation?
		// do nothing for now
	}

	@Override
	public void release(final SnomedIdentifier identifier) {
		reservedComponentIds.remove(identifier.toString());
	}

	@Override
	public void publish(final SnomedIdentifier identifier) {
		// TODO what to do in memory implementation?
		// do nothing for now
	}

	private String generateId(final String namespace, final ComponentCategory category) {
		String componentId = createComponentId(namespace, category);
		while (reservationService.isReserved(componentId)) {
			componentId = createComponentId(namespace, category);
		}

		return componentId;
	}

	private String createComponentId(final String namespace, final ComponentCategory category) {
		final StringBuilder builder = new StringBuilder();
		// generate the SCT Item ID
		builder.append(generationStrategy.generateItemId());

		// append namespace and the first part of the partition-identifier
		if (Strings.isNullOrEmpty(namespace)) {
			builder.append('0');
		} else {
			builder.append(namespace);
			builder.append('1');
		}

		// append the second part of the partition-identifier
		builder.append(category.ordinal());

		// calc check-digit
		builder.append(VerhoeffCheck.calculateChecksum(builder, false));

		return builder.toString();
	}

}<commit_msg>SO-1708 New skeleton methods to work with mem store.<commit_after>/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.snowowl.snomed.datastore.id.memory;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;

import com.b2international.commons.VerhoeffCheck;
import com.b2international.snowowl.core.exceptions.BadRequestException;
import com.b2international.snowowl.core.exceptions.NotImplementedException;
import com.b2international.snowowl.core.terminology.ComponentCategory;
import com.b2international.snowowl.snomed.datastore.SnomedTerminologyBrowser;
import com.b2international.snowowl.snomed.datastore.id.AbstractSnomedIdentifierServiceImpl;
import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifier;
import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifiers;
import com.b2international.snowowl.snomed.datastore.id.cis.IdentifierStatus;
import com.b2international.snowowl.snomed.datastore.id.cis.SctId;
import com.b2international.snowowl.snomed.datastore.id.gen.ItemIdGenerationStrategy;
import com.b2international.snowowl.snomed.datastore.id.reservations.ISnomedIdentiferReservationService;
import com.b2international.snowowl.snomed.datastore.id.store.SnomedIdentifierMemStore;
import com.google.common.base.Strings;
import com.google.inject.Provider;

/**
 * In memory implementation of the identifier service.
 * 
 * @since 4.5
 */
public class InMemorySnomedIdentifierServiceImpl extends AbstractSnomedIdentifierServiceImpl {

	private ISnomedIdentiferReservationService reservationService;
	private ItemIdGenerationStrategy generationStrategy;

	private SnomedIdentifierMemStore store = new SnomedIdentifierMemStore();

	public SctId getSctId(final String componentId) {
		return store.get(componentId);
	}

	public Collection<SctId> getSctIds() {
		return store.values();
	}

	public InMemorySnomedIdentifierServiceImpl(final ItemIdGenerationStrategy generationStrategy,
			final Provider<SnomedTerminologyBrowser> provider) {
		super(provider);
		this.generationStrategy = generationStrategy;
	}

	@Override
	public boolean includes(final SnomedIdentifier identifier) {
		return super.includes(identifier) || getSctId(identifier.toString()).getStatus() != IdentifierStatus.AVAILABLE.getSerializedName();
	}

	@Override
	public SnomedIdentifier generate(final String namespace, final ComponentCategory category) {
		checkNotNull(category, "Component category must not be null.");
		checkCategory(category);

		final String componentId = generateId(namespace, category);
		final SctId sctId = buildSctId(componentId, IdentifierStatus.ASSIGNED);
		store.put(componentId, sctId);

		return SnomedIdentifiers.of(componentId);
	}

	@Override
	public void register(final SnomedIdentifier identifier) {
		if (reservationService.isReserved(identifier.toString())) {
			// TODO change exception
			throw new RuntimeException("Component ID is already registered.");
		} else {
			final SctId sctId = buildSctId(identifier, IdentifierStatus.ASSIGNED);
			store.put(identifier.toString(), sctId);
		}
	}

	@Override
	public SnomedIdentifier reserve(final String namespace, final ComponentCategory category) {
		checkNotNull(category, "Component category must not be null.");
		checkCategory(category);

		final String componentId = generateId(namespace, category);
		final SctId sctId = buildSctId(componentId, IdentifierStatus.RESERVED);
		store.put(componentId, sctId);

		return SnomedIdentifiers.of(componentId);
	}

	@Override
	public void deprecate(final SnomedIdentifier identifier) {
		final SctId sctId = getSctId(identifier.toString());
		if (hasStatus(sctId, IdentifierStatus.ASSIGNED, IdentifierStatus.PUBLISHED)) {
		} else {
			throw new BadRequestException("");
		}
	}

	@Override
	public void release(final SnomedIdentifier identifier) {
		final SctId sctId = getSctId(identifier.toString());
		if (hasStatus(sctId, IdentifierStatus.ASSIGNED, IdentifierStatus.RESERVED)) {
		} else {
			throw new BadRequestException("");
		}
	}

	@Override
	public void publish(final SnomedIdentifier identifier) {
		final SctId sctId = getSctId(identifier.toString());
		if (hasStatus(sctId, IdentifierStatus.ASSIGNED, IdentifierStatus.PUBLISHED)) {
		} else {
			throw new BadRequestException("");
		}
	}

	private String generateId(final String namespace, final ComponentCategory category) {
		String componentId = createComponentId(namespace, category);
		while (reservationService.isReserved(componentId)) {
			componentId = createComponentId(namespace, category);
		}

		return componentId;
	}

	private String createComponentId(final String namespace, final ComponentCategory category) {
		final StringBuilder builder = new StringBuilder();
		// generate the SCT Item ID
		builder.append(generationStrategy.generateItemId());

		// append namespace and the first part of the partition-identifier
		if (Strings.isNullOrEmpty(namespace)) {
			builder.append('0');
		} else {
			builder.append(namespace);
			builder.append('1');
		}

		// append the second part of the partition-identifier
		builder.append(category.ordinal());

		// calc check-digit
		builder.append(VerhoeffCheck.calculateChecksum(builder, false));

		return builder.toString();
	}

	private SctId buildSctId(final String componentId, final IdentifierStatus status) {
		throw new NotImplementedException("Not implemented.");
	}

	private SctId buildSctId(final SnomedIdentifier identifier, final IdentifierStatus status) {
		throw new NotImplementedException("Not implemented.");
	}

	private boolean hasStatus(final SctId sctId, final IdentifierStatus... status) {
		throw new NotImplementedException("Not implemented.");
	}

}
{'fraction_non_alphanumeric': 0.08826734471485652, 'fraction_numerical': 0.005721031601888848, 'mean_word_length': 5.5592614651578325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5223517', 'n_tokens_mistral': 3266, 'n_tokens_neox': 2993, 'n_words': 778}