[vcs-log] convert VcsUserRegistryImpl to kotlin

This commit is contained in:
Julia Beliaeva
2018-06-25 18:45:27 +03:00
parent bc4c9e385b
commit 95d683dbb2
@@ -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<VcsUser>?
private val interner: Interner<VcsUser>
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<VcsUser> myPersistentEnumerator;
@NotNull private final Interner<VcsUser> 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<VcsUser> initEnumerator(@NotNull final File mapFile) {
private fun initEnumerator(mapFile: File): PersistentEnumeratorBase<VcsUser>? {
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<VcsUser>) {
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<VcsUser> {
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<VcsUser> 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<VcsUser> {
@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<VcsUser> getUsers() {
try {
Collection<VcsUser> 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<VcsUser> {
@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
}
}