Error 1169 - do not understand the problem

  • Thread starter Sebastian Nerger
  • Start date
S

Sebastian Nerger

I don't know what the compiler want...


Thanks!!!

Error 1169 Argument 1: cannot convert from
'biz.ritter.javapi.util.WeakHashMap<K,V>.Entry<K,V>.Type<V,K,V> [C:
\Develop\Projekte\J.net\JavApi\java.util.WeakHashMap.cs(66)]' to
'biz.ritter.javapi.util.WeakHashMap<K,V>.Entry<K,V>.Type<V,K,V> [C:
\Develop\Projekte\J.net\JavApi\java.util.WeakHashMap.cs(66)]' C:
\Develop\Projekte\J.net\JavApi\java.util.WeakHashMap.cs 531 44 JavApi



Source code parts - I hope it helps...
public class WeakHashMap<K, V> : AbstractMap<K, V>, Map<K, V>
{
internal sealed class Entry<K, V> :
java.lang.refj.WeakReference<K>,
MapNS.Entry<K, V>
{
//+ some more methods
internal interface Type<R, K, V>
{
R get(MapNS.Entry<K, V> entry);
}
}

class IAC_Values<V> : AbstractCollection<V>
{
//+some more methods
class ValuesEntryType<V> : Entry<K,V>.Type<V, K, V>
{
public V get(MapNS.Entry<K, V> entry)
{
return entry.getValue();
}
}
public override Iterator<V> iterator()
{
return new HashIterator<V>(new ValuesEntryType<V>(),
root);
}
}
class HashIterator<R> : Iterator<R>
{
private readonly WeakHashMap<K, V> root;
private int position = 0, expectedModCount;

readonly Entry<K, V>.Type<R, K, V> type;

internal HashIterator(Entry<K, V>.Type<R, K, V> type,
WeakHashMap<K, V> root)
{
this.type = type;
this.root = root;
expectedModCount = root.modCount;
}
//+some more methods
}
}
 
S

Sebastian Nerger

I don't know what the compiler want...
[...]
If that's not your problem, then you need to post a concise-but-complete
code example that reliably demonstrates the problem.  As near as I can
tell the code you posted doesn't even include the line of code that
causes the error, never mind is it complete.

Pete

I have the class only once in one (base) assembly. So here come the
complete long source...
thx

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
* See the License for the specific language governing permissions
and
* limitations under the License.
*
*/

using System;
using java = biz.ritter.javapi;

namespace biz.ritter.javapi.util
{

/**
* WeakHashMap is an implementation of Map with keys which are
WeakReferences. A
* key/value mapping is removed when the key is no longer referenced.
All
* optional operations (adding and removing) are supported. Keys and
values can
* be any objects. Note that the garbage collector acts similar to a
second
* thread on this collection, possibly removing keys.
*
* @since 1.2
* @see HashMap
* @see WeakReference
*/
public class WeakHashMap<K, V> : AbstractMap<K, V>, Map<K, V>
{

private static readonly int DEFAULT_SIZE = 16;

private readonly java.lang.refj.ReferenceQueue<K>
referenceQueue;

int elementCount;

Entry<K, V>[] elementData;

private readonly int loadFactor;

private int threshold;

volatile int modCount;

// Simple utility method to isolate unchecked cast for array
creation
private static Entry<K, V>[] newEntryArray<K, V>(int size)
{
return new Entry<K, V>[size];
}

internal sealed class Entry<K, V> :
java.lang.refj.WeakReference<K>,
MapNS.Entry<K, V>
{
internal int hash;

internal bool isNull;

internal V value;

internal Entry<K, V> next;
internal interface Type<R, K, V>
{
R get(MapNS.Entry<K, V> entry);
}
internal Entry(K key, V obj,
java.lang.refj.ReferenceQueue<K> queue)
: base(key, queue)
{
isNull = key == null;
hash = isNull ? 0 : key.GetHashCode();
value = obj;
}

public K getKey()
{
return base.get();
}

public V getValue()
{
return value;
}

public V setValue(V obj)
{
V result = value;
value = obj;
return result;
}

public override bool Equals(Object other)
{
if (!(other is MapNS.Entry<Object, Object>))
{
return false;
}
MapNS.Entry<Object, Object> entry =
(MapNS.Entry<Object, Object>)other;
Object key = base.get();
return (key == null ? key == entry.getKey() :
key.equals(entry.getKey())) &&
(value == null ? (Object)value ==
entry.getValue() : value.equals(entry.getValue()));
}

public override int GetHashCode()
{
return hash + (value == null ? 0 :
value.GetHashCode());
}

public override String ToString()
{
return base.get() + "=" + value; //$NON-NLS-1$
}
}

class HashIterator<R> : Iterator<R>
{
private readonly WeakHashMap<K, V> root;
private int position = 0, expectedModCount;

private Entry<K, V> currentEntry, nextEntry;

private K nextKey;

readonly Entry<K, V>.Type<R, K, V> type;

internal HashIterator(Entry<K, V>.Type<R, K, V> type,
WeakHashMap<K, V> root)
{
this.type = type;
this.root = root;
expectedModCount = root.modCount;
}

public bool hasNext()
{
if (nextEntry != null && (nextKey != null ||
nextEntry.isNull))
{
return true;
}
while (true)
{
if (nextEntry == null)
{
while (position < root.elementData.Length)
{
if ((nextEntry = root.elementData[position+
+]) != null)
{
break;
}
}
if (nextEntry == null)
{
return false;
}
}
// ensure key of next entry is not gc'ed
nextKey = nextEntry.get();
if (nextKey != null || nextEntry.isNull)
{
return true;
}
nextEntry = nextEntry.next;
}
}

public R next()
{
if (expectedModCount == root.modCount)
{
if (hasNext())
{
currentEntry = nextEntry;
nextEntry = currentEntry.next;
R result = type.get(currentEntry);
// free the key
nextKey = default(K);
return result;
}
throw new NoSuchElementException();
}
throw new ConcurrentModificationException();
}

public void remove()
{
if (expectedModCount == root.modCount)
{
if (currentEntry != null)
{
root.removeEntry(currentEntry);
currentEntry = null;
expectedModCount++;
// cannot poll() as that would change the
expectedModCount
}
else
{
throw new java.lang.IllegalStateException();
}
}
else
{
throw new ConcurrentModificationException();
}
}
}

/**
* Constructs a new empty {@code WeakHashMap} instance.
*/
public WeakHashMap() :
this(DEFAULT_SIZE)
{
}

/**
* Constructs a new {@code WeakHashMap} instance with the
specified
* capacity.
*
* @Param capacity
* the initial capacity of this map.
* @throws IllegalArgumentException
* if the capacity is less than zero.
*/
public WeakHashMap(int capacity)
{
if (capacity >= 0)
{
elementCount = 0;
elementData = newEntryArray<K, V>(capacity == 0 ? 1 :
capacity);
loadFactor = 7500; // Default load factor of 0.75
computeMaxSize();
referenceQueue = new
java.lang.refj.ReferenceQueue<K>();
}
else
{
throw new java.lang.IllegalArgumentException();
}
}

/**
* Constructs a new {@code WeakHashMap} instance with the
specified capacity
* and load factor.
*
* @Param capacity
* the initial capacity of this map.
* @Param loadFactor
* the initial load factor.
* @throws IllegalArgumentException
* if the capacity is less than zero or the load
factor is less
* or equal to zero.
*/
public WeakHashMap(int capacity, float loadFactor)
{
if (capacity >= 0 && loadFactor > 0)
{
elementCount = 0;
elementData = newEntryArray<K, V>(capacity == 0 ? 1 :
capacity);
this.loadFactor = (int)(loadFactor * 10000);
computeMaxSize();
referenceQueue = new
java.lang.refj.ReferenceQueue<K>();
}
else
{
throw new java.lang.IllegalArgumentException();
}
}

/**
* Constructs a new {@code WeakHashMap} instance containing
the mappings
* from the specified map.
*
* @Param map
* the mappings to add.
*/
public WeakHashMap(Map<K, V> map) :
this(map.size() < 6 ? 11 : map.size() * 2)
{
putAllImpl(map);
}

/**
* Removes all mappings from this map, leaving it empty.
*
* @see #isEmpty()
* @see #size()
*/
public override void clear()
{
if (elementCount > 0)
{
elementCount = 0;
Arrays<Entry<K, V>>.fill(elementData, null);
modCount++;
while (referenceQueue.poll() != null)
{
// do nothing
}
}
}

private void computeMaxSize()
{
threshold = (int)(elementData.LongLength * loadFactor /
10000);
}

/**
* Returns whether this map contains the specified key.
*
* @Param key
* the key to search for.
* @return {@code true} if this map contains the specified
key,
* {@code false} otherwise.
*/
public override bool containsKey(Object key)
{
return getEntry(key) != null;
}

/**
* Returns a set containing all of the mappings in this map.
Each mapping is
* an instance of {@link Map.Entry}. As the set is backed by
this map,
* changes in one will be reflected in the other. It does not
support adding
* operations.
*
* @return a set of the mappings.
*/
public override Set<MapNS.Entry<K, V>> entrySet()
{
poll();
return new IAC_EntrySet(this);
}
class IAC_EntrySet : AbstractSet<MapNS.Entry<K, V>>
{
private readonly WeakHashMap<K, V> root;
public IAC_EntrySet(WeakHashMap<K, V> root) { this.root =
root; }
public override int size()
{
return root.size();
}

public override void clear()
{
root.clear();
}

public override bool remove(Object obj)
{
if (contains(obj))
{
root
.remove(((MapNS.Entry<Object,
Object>)obj).getKey());
return true;
}
return false;
}

public override bool contains(Object obj)
{
if (obj is MapNS.Entry<K, V>)
{
Entry<K, V> entry = root.getEntry(((MapNS.Entry<K,
V>)obj)
.getKey());
if (entry != null)
{
Object key = entry.get();
if (key != null || entry.isNull)
{
return obj.equals(entry);
}
}
}
return false;
}

public override Iterator<MapNS.Entry<K, V>> iterator()
{
return new IAC_TypeImpl();
}
}
class IAC_TypeImpl : HashIterator<MapNS.Entry<K, V>>
{
public java.util.MapNS.Entry<K, V>
get(java.util.MapNS.Entry<K, V> entry)
{
return entry;
}
}

/**
* Returns a set of the keys contained in this map. The set is
backed by
* this map so changes to one are reflected by the other. The
set does not
* support adding.
*
* @return a set of the keys.
*/
public override Set<K> keySet()
{
poll();
if (keySetJ == null)
{
keySetJ = new IAC_KeySet<K>(this);
}
return keySetJ;
}
class IAC_KeySet<K> : AbstractSet<K>
{
private readonly WeakHashMap<K, V> root;
public IAC_KeySet(WeakHashMap<K, V> root) { this.root =
root; }
public override bool contains(Object obj)
{
return root.containsKey(obj);
}

public override int size()
{
return root.size();
}

public override void clear()
{
root.clear();
}

public override bool remove(Object key)
{
if (root.containsKey(key))
{
root.remove(key);
return true;
}
return false;
}

public override Iterator<K> iterator()
{
return new IAC_KeyHashIterator<K>();
}
class IAC_KeyHashIterator<K> : HashIterator<K>
{
public K get(MapNS.Entry<K, V> entry)
{
return entry.getKey();
}

}

public override Object[] toArray()
{
Collection<K> coll = new ArrayList<K>(size());

for (Iterator<K> iter = iterator(); iter.hasNext(); )
{
coll.add(iter.next());
}
return coll.toArray();
}

public override T[] toArray<T>(T[] contents)
{
Collection<K> coll = new ArrayList<K>(size());

for (Iterator<K> iter = iterator(); iter.hasNext(); )
{
coll.add(iter.next());
}
return coll.toArray(contents);
}
}



/**
* Returns a collection of the values contained in this map.
The collection
* is backed by this map so changes to one are reflected by
the other. The
* collection supports remove, removeAll, retainAll and clear
operations,
* and it does not support add or addAll operations.
* <p>
* This method returns a collection which is the subclass of
* AbstractCollection. The iterator method of this subclass
returns a
* "wrapper object" over the iterator of map's entrySet(). The
size method
* wraps the map's size method and the contains method wraps
the map's
* containsValue method.
* <p>
* The collection is created when this method is called at
first time and
* returned in response to all subsequent calls. This method
may return
* different Collection when multiple calls to this method,
since it has no
* synchronization performed.
*
* @return a collection of the values contained in this map.
*/
public override java.util.Collection<V> values()
{
poll();
if (valuesCollection == null)
{
valuesCollection = new IAC_Values<V>(this);
}
return valuesCollection;
}

class IAC_Values<V> : AbstractCollection<V>
{
private WeakHashMap<K, V> root;
public IAC_Values(WeakHashMap<K, V> root) { this.root =
root; }
public override int size()
{
return root.size();
}

public override void clear()
{
root.clear();
}

public override bool contains(Object obj)
{
return root.containsValue(obj);
}
class ValuesEntryType<V> : Entry<K, V>.Type<V, K, V>
{
public V get(MapNS.Entry<K, V> entry)
{
return entry.getValue();
}
}
public override Iterator<V> iterator()
{
return new HashIterator<V>(new ValuesEntryType<V>(),
root);
}
}


/**
* Returns the value of the mapping with the specified key.
*
* @Param key
* the key.
* @return the value of the mapping with the specified key, or
{@code null}
* if no mapping for the specified key is found.
*/
public override V get(Object key)
{
poll();
if (key != null)
{
int index = (key.GetHashCode() & 0x7FFFFFFF) %
elementData.Length;
Entry<K, V> entry = elementData[index];
while (entry != null)
{
if (key.equals(entry.get()))
{
return entry.value;
}
entry = entry.next;
}
return default(V);
}
Entry<K, V> entry2 = elementData[0];
while (entry2 != null)
{
if (entry2.isNull)
{
return entry2.value;
}
entry2 = entry2.next;
}
return default(V);
}

Entry<K, V> getEntry(Object key)
{
poll();
if (key != null)
{
int index = (key.GetHashCode() & 0x7FFFFFFF) %
elementData.Length;
Entry<K, V> entry = elementData[index];
while (entry != null)
{
if (key.equals(entry.get()))
{
return entry;
}
entry = entry.next;
}
return null;
}
Entry<K, V> entry2 = elementData[0];
while (entry2 != null)
{
if (entry2.isNull)
{
return entry2;
}
entry2 = entry2.next;
}
return null;
}

/**
* Returns whether this map contains the specified value.
*
* @Param value
* the value to search for.
* @return {@code true} if this map contains the specified
value,
* {@code false} otherwise.
*/
public override bool containsValue(Object value)
{
poll();
if (value != null)
{
for (int i = elementData.Length; --i >= 0; )
{
Entry<K, V> entry = elementData;
while (entry != null)
{
K key = entry.get();
if ((key != null || entry.isNull)
&& value.equals(entry.value))
{
return true;
}
entry = entry.next;
}
}
}
else
{
for (int i = elementData.Length; --i >= 0; )
{
Entry<K, V> entry = elementData;
while (entry != null)
{
K key = entry.get();
if ((key != null || entry.isNull) &&
entry.value == null)
{
return true;
}
entry = entry.next;
}
}
}
return false;
}

/**
* Returns the number of elements in this map.
*
* @return the number of elements in this map.
*/
public override bool isEmpty()
{
return size() == 0;
}

void poll()
{
Entry<K, V> toRemove;
while ((toRemove = (Entry<K, V>)referenceQueue.poll()) !=
null)
{
removeEntry(toRemove);
}
}

void removeEntry(Entry<K, V> toRemove)
{
Entry<K, V> entry, last = null;
int index = (toRemove.hash & 0x7FFFFFFF) %
elementData.Length;
entry = elementData[index];
// Ignore queued entries which cannot be found, the user
could
// have removed them before they were queued, i.e. using
clear()
while (entry != null)
{
if (toRemove == entry)
{
modCount++;
if (last == null)
{
elementData[index] = entry.next;
}
else
{
last.next = entry.next;
}
elementCount--;
break;
}
last = entry;
entry = entry.next;
}
}

/**
* Maps the specified key to the specified value.
*
* @Param key
* the key.
* @Param value
* the value.
* @return the value of any previous mapping with the
specified key or
* {@code null} if there was no mapping.
*/
public override V put(K key, V value)
{
poll();
int index = 0;
Entry<K, V> entry;
if (key != null)
{
index = (key.GetHashCode() & 0x7FFFFFFF) %
elementData.Length;
entry = elementData[index];
while (entry != null && !key.equals(entry.get()))
{
entry = entry.next;
}
}
else
{
entry = elementData[0];
while (entry != null && !entry.isNull)
{
entry = entry.next;
}
}
if (entry == null)
{
modCount++;
if (++elementCount > threshold)
{
rehash();
index = key == null ? 0 : (key.GetHashCode() &
0x7FFFFFFF)
% elementData.Length;
}
entry = new Entry<K, V>(key, value, referenceQueue);
entry.next = elementData[index];
elementData[index] = entry;
return default(V);
}
V result = entry.value;
entry.value = value;
return result;
}

private void rehash()
{
int length = elementData.Length << 1;
if (length == 0)
{
length = 1;
}
Entry<K, V>[] newData = newEntryArray<K, V>(length);
for (int i = 0; i < elementData.Length; i++)
{
Entry<K, V> entry = elementData;
while (entry != null)
{
int index = entry.isNull ? 0 : (entry.hash &
0x7FFFFFFF)
% length;
Entry<K, V> next = entry.next;
entry.next = newData[index];
newData[index] = entry;
entry = next;
}
}
elementData = newData;
computeMaxSize();
}

/**
* Copies all the mappings in the given map to this map. These
mappings will
* replace all mappings that this map had for any of the keys
currently in
* the given map.
*
* @Param map
* the map to copy mappings from.
* @throws NullPointerException
* if {@code map} is {@code null}.
*/
public override void putAll(Map<K, V> map)
{
putAllImpl(map);
}

/**
* Removes the mapping with the specified key from this map.
*
* @Param key
* the key of the mapping to remove.
* @return the value of the removed mapping or {@code null} if
no mapping
* for the specified key was found.
*/
public override V remove(Object key)
{
poll();
int index = 0;
Entry<K, V> entry, last = null;
if (key != null)
{
index = (key.GetHashCode() & 0x7FFFFFFF) %
elementData.Length;
entry = elementData[index];
while (entry != null && !key.equals(entry.get()))
{
last = entry;
entry = entry.next;
}
}
else
{
entry = elementData[0];
while (entry != null && !entry.isNull)
{
last = entry;
entry = entry.next;
}
}
if (entry != null)
{
modCount++;
if (last == null)
{
elementData[index] = entry.next;
}
else
{
last.next = entry.next;
}
elementCount--;
return entry.value;
}
return default(V);
}

/**
* Returns the number of elements in this map.
*
* @return the number of elements in this map.
*/
public override int size()
{
poll();
return elementCount;
}

private void putAllImpl(Map<K, V> map)
{
if (map.entrySet() != null)
{
base.putAll(map);
}
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top