VersionManagerImpl.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * 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 org.genesys.server.service.impl;

import java.util.Calendar;

import org.genesys.server.service.VersionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.vdurmont.semver4j.Semver;
import com.vdurmont.semver4j.Semver.SemverType;

/**
 * Manage version updates
 */
@Component
public class VersionManagerImpl implements VersionManager {
	private static final Logger LOG = LoggerFactory.getLogger(org.genesys.server.service.impl.VersionManagerImpl.class);

	@Override
	public String next(final String versionTag, final boolean major) {
		return major ? nextMajor(versionTag) : nextMinor(versionTag);
	}

	@Override
	public String nextMajor(final String versionTag) {
		Semver version = new Semver(versionTag, SemverType.LOOSE);
		LOG.debug("Source version {} = {}", versionTag, version.getValue());
		if (version.getMajor() > 1000) {
			// likely a year
			final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
			if (currentYear == version.getMajor()) {
				if (version.getMinor() == null) {
					return currentYear + ".1";
				} else {
					version = version.nextMinor();
				}
			} else {
				return "" + currentYear;
			}
		} else {
			version = version.nextMajor();
		}
		LOG.debug("Next version {} -> {}", versionTag, version.getValue());
		return version.getValue();
	}

	@Override
	public String nextMinor(final String versionTag) {
		Semver version = new Semver(versionTag, SemverType.LOOSE);
		LOG.debug("Source version {} = {}", versionTag, version.getValue());
		version = version.nextMinor();
		LOG.debug("Next version {} -> {}", versionTag, version.getValue());
		return version.getValue();
	}

}