Rewrite in kotlin

This commit is contained in:
Him188 2019-10-03 19:26:33 +08:00
parent 1e3db169b0
commit db77227d35
7 changed files with 13 additions and 366 deletions

View File

@ -2,7 +2,6 @@ package net.mamoe.mirai.network.protocol.tim.handler
import net.mamoe.mirai.network.LoginSession
import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
import net.mamoe.mirai.utils.MiraiSynchronizedLinkedList
import java.io.Closeable
/**
@ -27,7 +26,7 @@ fun PacketHandler.asNode(): PacketHandlerNode<PacketHandler> {
return PacketHandlerNode(this.javaClass, this)
}
class PacketHandlerList : MiraiSynchronizedLinkedList<PacketHandlerNode<*>>() {
class PacketHandlerList : MutableList<PacketHandlerNode<*>> by mutableListOf() {
fun <T : PacketHandler> get(clazz: Class<T>): T {
this.forEach {

View File

@ -1,18 +1,13 @@
package net.mamoe.mirai.network.protocol.tim.packet;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
package net.mamoe.mirai.network.protocol.tim.packet
/**
* @author Him188moe
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PacketId {
/**
* 获取用于识别的包 ID
*/
String value();
}
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE)
annotation class PacketId(
/**
* 用于识别的包 ID
*/
val value: String
)

View File

@ -5,4 +5,4 @@ import net.mamoe.mirai.contact.Contact
/**
* @author Him188moe
*/
class ContactList<C : Contact> : MiraiSynchronizedLinkedHashMap<Long, C>()
class ContactList<C : Contact> : MutableMap<Long, C> by mutableMapOf()

View File

@ -1,158 +0,0 @@
package net.mamoe.mirai.utils;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* 实现了可以直接被继承的 SynchronizedLinkedListMap<K,V>
*
* @param <K> the type of key
* @param <V> the type of value
*
* @author NaturalHG
*/
public class MiraiSynchronizedLinkedHashMap<K, V> extends AbstractMap<K, V> {
public MiraiSynchronizedLinkedHashMap() {
this.sortedMap = Collections.synchronizedMap(new LinkedHashMap<>());
}
protected final Map<K, V> sortedMap;
public MiraiSynchronizedLinkedHashMap(LinkedHashMap<K, V> map) {
this.sortedMap = Collections.synchronizedMap(map);
}
@Override
public int size() {
return sortedMap.size();
}
@Override
public boolean isEmpty() {
return sortedMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return sortedMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return sortedMap.containsValue(value);
}
@Override
public V get(Object key) {
return sortedMap.get(key);
}
@Override
public V put(K key, V value) {
return sortedMap.put(key,value);
}
@Override
public V remove(Object key) {
return sortedMap.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
sortedMap.putAll(m);
}
@Override
public void clear() {
sortedMap.clear();
}
@Override
public Set<K> keySet() {
return sortedMap.keySet();
}
@Override
public Collection<V> values() {
return sortedMap.values();
}
@Override
public Set<Entry<K, V>> entrySet() {
return sortedMap.entrySet();
}
@Override
public String toString() {
return this.sortedMap.toString();
}
@Override
public V getOrDefault(Object key, V defaultValue) {
return this.sortedMap.getOrDefault(key,defaultValue);
}
@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
this.sortedMap.forEach(action);
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return this.sortedMap.replace(key,oldValue,newValue);
}
@Override
public V replace(K key, V value) {
return this.sortedMap.replace(key,value);
}
@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
this.sortedMap.replaceAll(function);
}
@Override
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return this.sortedMap.compute(key,remappingFunction);
}
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
return this.sortedMap.computeIfAbsent(key,mappingFunction);
}
@Override
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return this.sortedMap.computeIfPresent(key,remappingFunction);
}
@Override
public int hashCode() {
return this.sortedMap.hashCode();
}
@Override
public V putIfAbsent(K key, V value) {
return this.sortedMap.putIfAbsent(key,value);
}
@Override
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
return this.sortedMap.merge(key,value,remappingFunction);
}
public boolean equals(MiraiSynchronizedLinkedHashMap o) {
return this.sortedMap.equals(o.sortedMap);
}
@Override
public boolean equals(Object o) {
return o instanceof MiraiSynchronizedLinkedHashMap ? this.equals((MiraiSynchronizedLinkedHashMap) o) : super.equals(o);
}
}

View File

@ -1,187 +0,0 @@
package net.mamoe.mirai.utils;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
/**
* @author Him188moe
*/
public class MiraiSynchronizedLinkedList<E> extends AbstractList<E> {
@SuppressWarnings("WeakerAccess")
protected final List<E> syncList;
public MiraiSynchronizedLinkedList() {
this.syncList = Collections.synchronizedList(new LinkedList<>());
}
public MiraiSynchronizedLinkedList(Collection<E> collection) {
this.syncList = Collections.synchronizedList(new LinkedList<>(collection));
}
@Override
public E get(int index) {
return this.syncList.get(index);
}
@Override
public void forEach(Consumer<? super E> action) {
this.syncList.forEach(action);
}
@Override
public Spliterator<E> spliterator() {
return this.syncList.spliterator();
}
@Override
public Stream<E> stream() {
return this.syncList.stream();
}
@Override
public Stream<E> parallelStream() {
return this.syncList.parallelStream();
}
@Override
public int size() {
return this.syncList.size();
}
@SuppressWarnings("SuspiciousToArrayCall")
@Override
public <T> T[] toArray(IntFunction<T[]> generator) {
return this.syncList.toArray(generator);
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
return this.syncList.removeIf(filter);
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
this.syncList.replaceAll(operator);
}
@Override
public void sort(Comparator<? super E> c) {
this.syncList.sort(c);
}
@Override
public boolean add(E e) {
return this.syncList.add(e);
}
@Override
public E set(int index, E element) {
return this.syncList.set(index, element);
}
@Override
public void add(int index, E element) {
this.syncList.add(index, element);
}
@Override
public E remove(int index) {
return this.syncList.remove(index);
}
@Override
public int indexOf(Object o) {
return this.syncList.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return this.syncList.lastIndexOf(o);
}
@Override
public void clear() {
this.syncList.clear();
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
return this.syncList.addAll(index, c);
}
@Override
public Iterator<E> iterator() {
return this.syncList.iterator();
}
@Override
public ListIterator<E> listIterator() {
return this.syncList.listIterator();
}
@Override
public ListIterator<E> listIterator(int index) {
return this.syncList.listIterator(index);
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
return this.syncList.subList(fromIndex, toIndex);
}
@Override
public int hashCode() {
return this.syncList.hashCode();
}
@Override
public boolean isEmpty() {
return this.syncList.isEmpty();
}
@Override
public boolean contains(Object o) {
return this.syncList.contains(o);
}
@Override
public Object[] toArray() {
return this.syncList.toArray();
}
@SuppressWarnings("SuspiciousToArrayCall")
@Override
public <T> T[] toArray(T[] a) {
return this.syncList.toArray(a);
}
@Override
public boolean remove(Object o) {
return this.syncList.remove(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return this.syncList.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends E> c) {
return this.syncList.addAll(c);
}
@Override
public boolean removeAll(Collection<?> c) {
return this.syncList.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return this.syncList.retainAll(c);
}
}

View File

@ -22,7 +22,7 @@ class MiraiConfig(private val root: File) : MiraiConfigSection<Any>(parse(Object
val dumperOptions = DumperOptions()
dumperOptions.defaultFlowStyle = DumperOptions.FlowStyle.BLOCK
val yaml = Yaml(dumperOptions)
val content = yaml.dump(this.sortedMap)
val content = yaml.dump(this)
try {
ByteArrayInputStream(content.toByteArray()).transferTo(FileOutputStream(this.root))
} catch (e: IOException) {

View File

@ -1,7 +1,5 @@
package net.mamoe.mirai.utils.config;
import net.mamoe.mirai.utils.MiraiSynchronizedLinkedHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
@ -10,7 +8,7 @@ import java.util.function.Supplier;
/**
* @author NaturalHG
*/
public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedHashMap<String, T> {
public class MiraiConfigSection<T> extends LinkedHashMap<String, T> {
public MiraiConfigSection(){
super();