/** *Generatesaworldwideuniqueidentifierstring. * *<p>Itisguaranteedthateveryinvocationofthismethodgeneratesanew *ID,whichisuniquewithintheVM.Thequalityof“worldwide *unique”willdependontheactualimplementation,youshouldlook *atthesourcetodetermineifitmeetsyourrequirements.</p> * *@returnaunique<code>String</code>
*/ publicstatic String getUniqueKey() { synchronized (uniqueKeyLock) { if (uniqueKeyCount == Long.MAX_VALUE) { long time; for (time = System.currentTimeMillis(); time == uniqueKeyTime;)
{ // Conservatively sleep for 100 millisecond to wait for // System.currentTimeMillis() to change: try { Thread.sleep(100);
} catch (InterruptedException e) { Thread.currentThread().interrupt();
}
}
uniqueKeyTime = time;
uniqueKeyCount = Long.MIN_VALUE;
} return uniqueKeyHostPrefix + Long.toString(uniqueKeyTime, 16) + ":"
+ Long.toString(uniqueKeyCount++, 16);
}
}
/** *Generatesaworldwideuniqueobjectidentifier(OID)forthegiven *Javaobject. * *<p>Itisguaranteedthatsubsequentcallstothismethodwiththesame *JavaobjectwillgivethesameID.</p> * *<p>Thismethodisgenerallyoflittleuseforclientcode.Itshouldbe *consideredamistakethatthismethodispublishedatall.</p> * *@paramobjectanyobjectforwhichanOIDshallbegenerated;mustnotbe *<code>null</code> *@returnthegeneratedOID *@seecom.sun.star.uno.IQueryInterface#getOid
*/ publicstatic String generateOid(Object object) {
String oid = null; if (object instanceof IQueryInterface) {
oid = ((IQueryInterface) object).getOid();
} if (oid == null) { synchronized (oidMap) {
oid = oidMap.get(object); if (oid == null) {
oid = UUID.randomUUID().toString() + oidSuffix;
oidMap.put(object, oid);
}
}
} return oid;
}
/** *QueriesthegivenUNOobjectforthegivenUNOinterfacetype. * *<p>Thismethodreturns<code>null</code>incasethegivenUNOobject *doesnotsupportthegivenUNOinterfacetype(orisitself *<code>null</code>).Otherwise,areferencetoaJavaobjectimplementing *theJavainterfacetypecorrespondingtothegivenUNOinterfaceis *returned.Inthelattercase,itisunspecifiedwhetherthereturned *Javaobjectisthesameasthegivenobject,orisanotherfacetofthat *UNOobject.</p> * *@paramtypetherequestedUNOinterfacetype;mustbea<code>Type</code> *objectrepresentingaUNOinterfacetype *@paramobjectareferencetoanyJavaobjectrepresenting(afacetof)a *UNOobject;maybe<code>null</code> *@returnareferencetotherequestedUNOinterfacetypeifavailable, *otherwise<code>null</code> *@seecom.sun.star.uno.IQueryInterface#queryInterface
*/ publicstatic Object queryInterface(Type type, Object object) { // Gracefully handle those situations where the passed in UNO object is // wrapped in an Any. Strictly speaking, such a situation constitutes a // bug, but it is anticipated that such situations will arise quite // often in practice (especially since UNO Anys containing an XInterface // reference are not wrapped in a Java Any, but UNO Anys containing any // other interface reference are wrapped in a Java Any, which can lead // to confusion). if (object instanceof Any) {
Any a = (Any) object; if (a.getType().getTypeClass() == TypeClass.INTERFACE) {
object = a.getObject();
}
} if (object instanceof IQueryInterface) {
object = ((IQueryInterface) object).queryInterface(type); if (object instanceof Any) {
Any a = (Any) object;
object = a.getType().getTypeClass() == TypeClass.INTERFACE
? a.getObject() : null;
}
} // Ensure that the object implements the requested interface type: Class<?> c = type.getZClass(); if (c == null || !c.isInstance(object)) {
object = null;
} return object;
}
@return<code>true</code>ifandonlyifthetwoargumentsrepresent equalUNOvalues.
*/ publicstaticboolean areSame(Object any1, Object any2) {
Any a1 = Any.complete(any1);
Any a2 = Any.complete(any2);
Type t = a1.getType(); if (!a2.getType().equals(t)) { returnfalse;
}
Object v1 = a1.getObject();
Object v2 = a2.getObject(); switch (t.getTypeClass().getValue()) { case TypeClass.VOID_value: returntrue; case TypeClass.BOOLEAN_value: case TypeClass.BYTE_value: case TypeClass.SHORT_value: case TypeClass.UNSIGNED_SHORT_value: case TypeClass.LONG_value: case TypeClass.UNSIGNED_LONG_value: case TypeClass.HYPER_value: case TypeClass.UNSIGNED_HYPER_value: case TypeClass.FLOAT_value: case TypeClass.DOUBLE_value: case TypeClass.CHAR_value: case TypeClass.STRING_value: case TypeClass.TYPE_value: return v1.equals(v2); case TypeClass.SEQUENCE_value: int n = Array.getLength(v1); if (n != Array.getLength(v2)) { returnfalse;
} for (int i = 0; i < n; ++i) { // Recursively using areSame on Java values that are (boxed) // elements of Java arrays representing UNO sequence values, // instead of on Java values that are representations of UNO ANY // values, works by chance: if (!areSame(Array.get(v1, i), Array.get(v2, i))) { returnfalse;
}
} returntrue; case TypeClass.ENUM_value: return v1 == v2; case TypeClass.STRUCT_value: case TypeClass.EXCEPTION_value:
FieldDescription[] fs; try {
fs = TypeDescription.getTypeDescription(t).
getFieldDescriptions();
} catch (ClassNotFoundException e) { thrownew java.lang.RuntimeException(e);
} for (int i = 0; i< fs.length; ++i) {
Type ft = new Type(fs[i].getTypeDescription()); try { // Recursively using areSame on Java values that are (boxed) // fields of Java classes representing UNO struct or // exception values, instead of on Java values that are // representations of UNO ANY values, works by chance: if (!areSame(
completeValue(ft, fs[i].getField().get(v1)),
completeValue(ft, fs[i].getField().get(v2))))
{ returnfalse;
}
} catch (IllegalAccessException e) { thrownew java.lang.RuntimeException(e);
}
} returntrue; case TypeClass.INTERFACE_value: return v1 == v2
|| (v1 instanceof IQueryInterface
&& ((IQueryInterface) v1).isSame(v2))
|| (v2 instanceof IQueryInterface
&& ((IQueryInterface) v2).isSame(v1)); default: thrownew java.lang.RuntimeException( "com.sun.star.uno.Any has bad com.sun.star.uno.TypeClass");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.