Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.api.interop.java;
import static com.oracle.truffle.api.interop.ForeignAccess.sendGetSize;
import static com.oracle.truffle.api.interop.ForeignAccess.sendHasKeys;
import static com.oracle.truffle.api.interop.ForeignAccess.sendHasSize;
import static com.oracle.truffle.api.interop.ForeignAccess.sendKeyInfo;
import static com.oracle.truffle.api.interop.ForeignAccess.sendKeys;
import static com.oracle.truffle.api.interop.ForeignAccess.sendRead;
import static com.oracle.truffle.api.interop.ForeignAccess.sendRemove;
import static com.oracle.truffle.api.interop.ForeignAccess.sendWrite;
import java.lang.reflect.Type;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.impl.Accessor.EngineSupport;
import com.oracle.truffle.api.interop.KeyInfo;
import com.oracle.truffle.api.interop.Message;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.ConditionProfile;
class TruffleMap extends AbstractMap {
final Object languageContext;
final TruffleObject guestObject;
final TruffleMapCache cache;
private final boolean includeInternal;
TruffleMap(Object languageContext, TruffleObject obj, Class keyClass, Class valueClass, Type valueType) {
this.guestObject = obj;
this.languageContext = languageContext;
this.includeInternal = false;
this.cache = TruffleMapCache.lookup(languageContext, obj.getClass(), keyClass, valueClass, valueType);
}
private TruffleMap(TruffleMap map, boolean includeInternal) {
this.guestObject = map.guestObject;
this.cache = map.cache;
this.languageContext = map.languageContext;
this.includeInternal = includeInternal;
}
static Map create(Object languageContext, TruffleObject foreignObject, boolean implementsFunction, Class keyClass, Class valueClass, Type valueType) {
if (implementsFunction) {
return new FunctionTruffleMap<>(languageContext, foreignObject, keyClass, valueClass, valueType);
} else {
return new TruffleMap<>(languageContext, foreignObject, keyClass, valueClass, valueType);
}
}
TruffleMap cloneInternal(boolean includeInternalKeys) {
return new TruffleMap<>(this, includeInternalKeys);
}
@Override
public boolean containsKey(Object key) {
return (boolean) cache.containsKey.call(languageContext, guestObject, key);
}
@SuppressWarnings("unchecked")
@Override
public Set> entrySet() {
return (Set>) cache.entrySet.call(languageContext, guestObject, this, includeInternal);
}
@SuppressWarnings("unchecked")
@Override
public V get(Object key) {
return (V) cache.get.call(languageContext, guestObject, key);
}
@SuppressWarnings("unchecked")
@Override
public V put(K key, V value) {
return (V) cache.put.call(languageContext, guestObject, key, value);
}
@SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
return (V) cache.remove.call(languageContext, guestObject, key);
}
@Override
public String toString() {
EngineSupport engine = JavaInteropAccessor.ACCESSOR.engine();
if (engine != null && languageContext != null) {
try {
return engine.toHostValue(guestObject, languageContext).toString();
} catch (UnsupportedOperationException e) {
return super.toString();
}
} else {
return super.toString();
}
}
private final class LazyEntries extends AbstractSet> {
private final List> props;
private final int keysSize;
private final int elemSize;
LazyEntries(List> keys, int keysSize, int elemSize) {
assert keys != null || keysSize == 0;
this.props = keys;
this.keysSize = keysSize;
this.elemSize = elemSize;
}
@Override
public Iterator> iterator() {
if (keysSize > 0 && elemSize > 0) {
return new CombinedIterator();
} else if (keysSize > 0) {
return new LazyKeysIterator();
} else {
return new ElementsIterator();
}
}
@Override
public int size() {
return ((props != null) ? props.size() : keysSize) + elemSize;
}
@Override
public boolean contains(Object o) {
return containsKey(o);
}
@Override
@SuppressWarnings("unchecked")
public boolean remove(Object o) {
if (o instanceof Entry) {
Entry