From 95d683dbb2ca9e9947a4810bb4b82baf6f56b01f Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Sun, 24 Jun 2018 00:40:23 +0300 Subject: [PATCH] [vcs-log] convert VcsUserRegistryImpl to kotlin --- .../vcs/log/data/VcsUserRegistryImpl.kt | 227 ++++++++---------- 1 file changed, 101 insertions(+), 126 deletions(-) diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/data/VcsUserRegistryImpl.kt b/platform/vcs-log/impl/src/com/intellij/vcs/log/data/VcsUserRegistryImpl.kt index 83ed26e6722e..f5c6200d908d 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/data/VcsUserRegistryImpl.kt +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/data/VcsUserRegistryImpl.kt @@ -13,155 +13,130 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.vcs.log.data; +package com.intellij.vcs.log.data -import com.intellij.openapi.Disposable; -import com.intellij.openapi.application.PathManager; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.project.Project; -import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.containers.Interner; -import com.intellij.util.io.*; -import com.intellij.vcs.log.VcsUser; -import com.intellij.vcs.log.VcsUserRegistry; -import com.intellij.vcs.log.impl.VcsUserImpl; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.File; -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.Set; +import com.intellij.openapi.Disposable +import com.intellij.openapi.application.PathManager +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.project.Project +import com.intellij.util.containers.ContainerUtil +import com.intellij.util.containers.Interner +import com.intellij.util.io.* +import com.intellij.vcs.log.VcsUser +import com.intellij.vcs.log.VcsUserRegistry +import com.intellij.vcs.log.impl.VcsUserImpl +import java.io.DataInput +import java.io.DataOutput +import java.io.File +import java.io.IOException /** * */ -public class VcsUserRegistryImpl implements Disposable, VcsUserRegistry { +class VcsUserRegistryImpl internal constructor(project: Project) : Disposable, VcsUserRegistry { + private val persistentEnumerator: PersistentEnumeratorBase? + private val interner: Interner - private static final File USER_CACHE_APP_DIR = new File(PathManager.getSystemPath(), "vcs-users"); - private static final Logger LOG = Logger.getInstance(VcsUserRegistryImpl.class); - private static final int STORAGE_VERSION = 2; - private static final PersistentEnumeratorBase.DataFilter ACCEPT_ALL_DATA_FILTER = id -> true; - - @Nullable private final PersistentEnumeratorBase myPersistentEnumerator; - @NotNull private final Interner myInterner; - - VcsUserRegistryImpl(@NotNull Project project) { - final File mapFile = new File(USER_CACHE_APP_DIR, project.getLocationHash() + "." + STORAGE_VERSION); - myPersistentEnumerator = initEnumerator(mapFile); - myInterner = new Interner<>(); + init { + val mapFile = File(USER_CACHE_APP_DIR, project.locationHash + "." + STORAGE_VERSION) + persistentEnumerator = initEnumerator(mapFile) + interner = Interner() } - @Nullable - private PersistentEnumeratorBase initEnumerator(@NotNull final File mapFile) { + private fun initEnumerator(mapFile: File): PersistentEnumeratorBase? { + return try { + IOUtil.openCleanOrResetBroken({ + PersistentBTreeEnumerator(mapFile, MyDescriptor(), Page.PAGE_SIZE, null, + STORAGE_VERSION) + }, mapFile) + } + catch (e: IOException) { + LOG.warn(e) + null + } + } + + override fun createUser(name: String, email: String): VcsUser { + synchronized(interner) { + return interner.intern(VcsUserImpl(name, email)) + } + } + + fun addUser(user: VcsUser) { try { - return IOUtil.openCleanOrResetBroken(() -> new PersistentBTreeEnumerator<>(mapFile, new MyDescriptor(), Page.PAGE_SIZE, null, - STORAGE_VERSION), mapFile); + persistentEnumerator?.enumerate(user) } - catch (IOException e) { - LOG.warn(e); - return null; + catch (e: IOException) { + LOG.warn(e) + } + + } + + fun addUsers(users: Collection) { + for (user in users) { + addUser(user) } } - @NotNull - @Override - public VcsUser createUser(@NotNull String name, @NotNull String email) { - synchronized (myInterner) { - return myInterner.intern(new VcsUserImpl(name, email)); + override fun getUsers(): Set { + return try { + persistentEnumerator?.let { ContainerUtil.newHashSet(it.getAllDataObjects { _ -> true }) } ?: emptySet() + } + catch (e: IOException) { + LOG.warn(e) + emptySet() } } - public void addUser(@NotNull VcsUser user) { + fun flush() { + persistentEnumerator?.force() + } + + override fun dispose() { try { - if (myPersistentEnumerator != null) { - myPersistentEnumerator.enumerate(user); - } + persistentEnumerator?.close() } - catch (IOException e) { - LOG.warn(e); + catch (e: IOException) { + LOG.warn(e) } } - public void addUsers(@NotNull Collection users) { - for (VcsUser user : users) { - addUser(user); + @Throws(IOException::class) + fun getUserId(user: VcsUser): Int { + return persistentEnumerator?.enumerate(user) ?: -1 + } + + @Throws(IOException::class) + fun getUserById(userId: Int?): VcsUser? { + return persistentEnumerator?.valueOf(userId!!) + } + + private inner class MyDescriptor : KeyDescriptor { + @Throws(IOException::class) + override fun save(out: DataOutput, value: VcsUser) { + IOUtil.writeUTF(out, value.name) + IOUtil.writeUTF(out, value.email) + } + + @Throws(IOException::class) + override fun read(`in`: DataInput): VcsUser { + val name = IOUtil.readUTF(`in`) + val email = IOUtil.readUTF(`in`) + return createUser(name, email) + } + + override fun getHashCode(value: VcsUser): Int { + return value.hashCode() + } + + override fun isEqual(val1: VcsUser, val2: VcsUser): Boolean { + return val1 == val2 } } - @Override - @NotNull - public Set getUsers() { - try { - Collection users = myPersistentEnumerator != null ? - myPersistentEnumerator.getAllDataObjects(ACCEPT_ALL_DATA_FILTER) : - Collections.emptySet(); - return ContainerUtil.newHashSet(users); - } - catch (IOException e) { - LOG.warn(e); - return Collections.emptySet(); - } - } - - public void flush() { - if (myPersistentEnumerator != null) { - myPersistentEnumerator.force(); - } - } - - @Override - public void dispose() { - try { - if (myPersistentEnumerator != null) { - myPersistentEnumerator.close(); - } - } - catch (IOException e) { - LOG.warn(e); - } - } - - public int getUserId(@NotNull VcsUser user) throws IOException { - if (myPersistentEnumerator != null) { - return myPersistentEnumerator.enumerate(user); - } - return -1; - } - - @Nullable - public VcsUser getUserById(Integer userId) throws IOException { - if (myPersistentEnumerator != null) { - return myPersistentEnumerator.valueOf(userId); - } - return null; - } - - private class MyDescriptor implements KeyDescriptor { - @Override - public void save(@NotNull DataOutput out, VcsUser value) throws IOException { - IOUtil.writeUTF(out, value.getName()); - IOUtil.writeUTF(out, value.getEmail()); - } - - @Override - public VcsUser read(@NotNull DataInput in) throws IOException { - String name = IOUtil.readUTF(in); - String email = IOUtil.readUTF(in); - return createUser(name, email); - } - - @Override - public int getHashCode(VcsUser value) { - return value.hashCode(); - } - - @Override - public boolean isEqual(VcsUser val1, VcsUser val2) { - return val1.equals(val2); - } + companion object { + private val LOG = Logger.getInstance(VcsUserRegistryImpl::class.java) + private val USER_CACHE_APP_DIR = File(PathManager.getSystemPath(), "vcs-users") + private const val STORAGE_VERSION = 2 } }