class ApiRepoVersionServiceImpl implements ApiRepoVersionService {

	@Override
	public Optional<ApiRepoVersion> findLatestStableVersion(Integer apiRepoId) {
		List<ApiRepoVersion> allVersions = apiRepoVersionDao.findAllByApiRepoId(apiRepoId);
		if(allVersions.isEmpty()) {
			return Optional.empty();
		}

		// 要先过滤掉 master 版本
		List<ApiRepoVersion> filtered = allVersions
			.stream()
			.filter(apiRepoVersion -> !apiRepoVersion.getVersion().equals(Constants.MASTER))
			.collect(Collectors.toList());
		if(filtered.isEmpty()) {
			return Optional.empty();
		}
		// 最新版本在最前面
		filtered.sort(new Comparator<ApiRepoVersion>() {
			@Override
			public int compare(ApiRepoVersion version1, ApiRepoVersion version2) {
				return Version.compare(Version.parseVersion(version2.getVersion()), Version.parseVersion(version1.getVersion()));
			}
		});
		return Optional.of(filtered.get(0));
	}

}