extensions.st
branchcvs_MAIN
changeset 3666 abc22951d4df
parent 3644 4fb87430526a
child 3670 293cd0b49165
equal deleted inserted replaced
3665:a6497adefa64 3666:abc22951d4df
   949 
   949 
   950     "Created: / 24-02-2012 / 19:41:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   950     "Created: / 24-02-2012 / 19:41:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   951     "Modified: / 07-05-2013 / 11:21:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   951     "Modified: / 07-05-2013 / 11:21:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   952 ! !
   952 ! !
   953 
   953 
       
   954 !(Java classForName:'JAVA::java::lang::Byte') class  methodsFor:'* proxies *'!
       
   955 
       
   956 javaWrap: object
       
   957 	|inst|
       
   958     inst := self basicNew.
       
   959     inst perform: #'<init>(B)V' with: object.
       
   960 	^ inst.
       
   961 ! !
       
   962 
       
   963 !(Java classForName:'JAVA::java::lang::Byte') class  methodsFor:'* proxies *'!
       
   964 
       
   965 javaWrapRequired
       
   966     "Returns true, if a #javaWrap: has to be called
       
   967      prior an instance of Smalltalk object is passed 
       
   968      as an argument to Java method, whoose formal tyoe
       
   969      is the receiver."
       
   970 
       
   971     ^true
       
   972 ! !
       
   973 
       
   974 !(Java classForName:'JAVA::java::lang::Double') class  methodsFor:'queries'!
       
   975 
       
   976 javaWrap: object
       
   977 	|inst|
       
   978     inst := self basicNew.
       
   979     inst perform: #'<init>(D)V' with: object with: nil.
       
   980 	^ inst.
       
   981 ! !
       
   982 
       
   983 !(Java classForName:'JAVA::java::lang::Double') class  methodsFor:'queries'!
       
   984 
       
   985 javaWrapRequired
       
   986     "Returns true, if a #javaWrap: has to be called
       
   987      prior an instance of Smalltalk object is passed 
       
   988      as an argument to Java method, whoose formal tyoe
       
   989      is the receiver."
       
   990 
       
   991     ^true
       
   992 ! !
       
   993 
       
   994 !(Java classForName:'JAVA::java::lang::Float') class  methodsFor:'queries'!
       
   995 
       
   996 javaWrap: object
       
   997 	|inst|
       
   998     inst := self basicNew.
       
   999     inst perform: #'<init>(F)V' with: object.
       
  1000 	^ inst.
       
  1001 ! !
       
  1002 
       
  1003 !(Java classForName:'JAVA::java::lang::Float') class  methodsFor:'queries'!
       
  1004 
       
  1005 javaWrapRequired
       
  1006     "Returns true, if a #javaWrap: has to be called
       
  1007      prior an instance of Smalltalk object is passed 
       
  1008      as an argument to Java method, whoose formal tyoe
       
  1009      is the receiver."
       
  1010 
       
  1011     ^true
       
  1012 ! !
       
  1013 
       
  1014 !(Java classForName:'JAVA::java::lang::Integer') class  methodsFor:'queries'!
       
  1015 
       
  1016 javaWrap: object
       
  1017 	|inst|
       
  1018     inst := self basicNew.
       
  1019 	inst perform: #'<init>(I)V' with: object.
       
  1020 	^ inst.
       
  1021 ! !
       
  1022 
       
  1023 !(Java classForName:'JAVA::java::lang::Integer') class  methodsFor:'queries'!
       
  1024 
       
  1025 javaWrapRequired
       
  1026     "Returns true, if a #javaWrap: has to be called
       
  1027      prior an instance of Smalltalk object is passed 
       
  1028      as an argument to Java method, whoose formal tyoe
       
  1029      is the receiver."
       
  1030 
       
  1031     ^true
       
  1032 ! !
       
  1033 
       
  1034 !(Java classForName:'JAVA::java::lang::Long') class  methodsFor:'queries'!
       
  1035 
       
  1036 javaWrap: object
       
  1037 	|inst|
       
  1038     inst := self basicNew.
       
  1039     inst perform: #'<init>(J)V' with: object with: nil.
       
  1040 	^ inst.
       
  1041 ! !
       
  1042 
       
  1043 !(Java classForName:'JAVA::java::lang::Long') class  methodsFor:'queries'!
       
  1044 
       
  1045 javaWrapRequired
       
  1046     "Returns true, if a #javaWrap: has to be called
       
  1047      prior an instance of Smalltalk object is passed
       
  1048      as an argument to Java method, whoose formal tyoe
       
  1049      is the receiver."
       
  1050 
       
  1051     ^true
       
  1052 ! !
       
  1053 
       
  1054 !(Java classForName:'JAVA::java::lang::Short') class  methodsFor:'queries'!
       
  1055 
       
  1056 javaWrap: object
       
  1057 	|inst|
       
  1058     inst := self basicNew.
       
  1059     inst perform: #'<init>(S)V' with: object.
       
  1060 	^ inst.
       
  1061 ! !
       
  1062 
       
  1063 !(Java classForName:'JAVA::java::lang::Short') class  methodsFor:'queries'!
       
  1064 
       
  1065 javaWrapRequired
       
  1066     "Returns true, if a #javaWrap: has to be called
       
  1067      prior an instance of Smalltalk object is passed 
       
  1068      as an argument to Java method, whoose formal tyoe
       
  1069      is the receiver."
       
  1070 
       
  1071     ^true
       
  1072 ! !
       
  1073 
       
  1074 !(Java classForName:'JAVA::java::lang::String') class  methodsFor:'queries'!
       
  1075 
       
  1076 javaWrap: object
       
  1077     | jstring |
       
  1078 
       
  1079     object isNil ifTrue:[ ^ nil ].
       
  1080     jstring := self basicNew.
       
  1081     jstring perform: #'<init>([C)V' with: object.
       
  1082     ^jstring
       
  1083 ! !
       
  1084 
       
  1085 !(Java classForName:'JAVA::java::lang::String') class  methodsFor:'queries'!
       
  1086 
       
  1087 javaWrapRequired
       
  1088     "Returns true, if a #javaWrap: has to be called
       
  1089      prior an instance of Smalltalk object is passed 
       
  1090      as an argument to Java method, whoose formal tyoe
       
  1091      is the receiver."
       
  1092 
       
  1093     ^true
       
  1094 ! !
       
  1095 
       
  1096 !(Java classForName:'java.lang.Throwable') methodsFor:'others'!
       
  1097 
       
  1098 description
       
  1099     ^self getMessage
       
  1100 ! !
       
  1101 
       
  1102 !(Java classForName:'java.lang.Throwable') methodsFor:'others'!
       
  1103 
       
  1104 isJavaLangThrowable
       
  1105     ^true
       
  1106 ! !
       
  1107 
       
  1108 !(Java classForName:'java.util.AbstractList') methodsFor:'* instance *'!
       
  1109 
       
  1110 at: index
       
  1111 
       
  1112     "/ Defined here (in addition to List iface) to overwrite
       
  1113     "/ Object>>at:
       
  1114     ^ self get: index - 1.
       
  1115 ! !
       
  1116 
       
  1117 !(Java classForName:'java.util.AbstractList') methodsFor:'* instance *'!
       
  1118 
       
  1119 inspectorClass
       
  1120     ^JavaListInspectorView
       
  1121 ! !
       
  1122 
       
  1123 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1124 
       
  1125 anElement
       
  1126     "return any element from the collection, 
       
  1127      report an error if there is none"
       
  1128 
       
  1129     self do: [:each | ^ each].
       
  1130     Smalltalk::Collection emptyCollectionSignal raise.
       
  1131 ! !
       
  1132 
       
  1133 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1134 
       
  1135 collect:aBlock
       
  1136     "for each element in the receiver, evaluate the argument, aBlock
       
  1137      and return a new collection with the results"
       
  1138 
       
  1139     |newCollection|
       
  1140 
       
  1141     newCollection := self species new.
       
  1142     self do:[:element | newCollection add:(aBlock value:element)].
       
  1143     ^ newCollection
       
  1144 
       
  1145     "
       
  1146      #(1 2 3 4) collect:[:n | n * 2]  
       
  1147     "
       
  1148 ! !
       
  1149 
       
  1150 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1151 
       
  1152 detect:aBlock
       
  1153     "evaluate the argument, aBlock for each element in the receiver until
       
  1154      the block returns true; in this case return the element which caused
       
  1155      the true evaluation.
       
  1156      If none of the evaluations returns true, report an error"
       
  1157 
       
  1158     ^ self detect:aBlock ifNone:[self errorNotFound]
       
  1159 
       
  1160     "
       
  1161      #(1 2 3 4) detect:[:n | n odd]   
       
  1162      #(2 4 6 8) detect:[:n | n odd]  
       
  1163     "
       
  1164 ! !
       
  1165 
       
  1166 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1167 
       
  1168 detect:aOneArgBlock ifNone:exceptionBlock
       
  1169     "evaluate the argument, aBlock for each element in the receiver until
       
  1170      the block returns true; in this case return the element which caused
       
  1171      the true evaluation.
       
  1172      If none of the evaluations returns true, return the result of the
       
  1173      evaluation of the exceptionBlock"
       
  1174 
       
  1175     self do:[:each | 
       
  1176         (aOneArgBlock value:each) ifTrue:[^ each].
       
  1177     ].
       
  1178     ^ exceptionBlock value
       
  1179 
       
  1180     "
       
  1181      #(1 2 3 4) detect:[:n | n odd] ifNone:['sorry']    
       
  1182      #(2 4 6 8) detect:[:n | n odd] ifNone:['sorry']     
       
  1183     "
       
  1184 
       
  1185     "Modified: / 13-09-2006 / 11:17:42 / cg"
       
  1186 ! !
       
  1187 
       
  1188 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1189 
       
  1190 do: aBlock
       
  1191     "Iterate over collection evaluating given block with each element"
       
  1192 
       
  1193     | iterator |
       
  1194 
       
  1195     iterator := self iterator.
       
  1196     [ iterator hasNext ] whileTrue:[
       
  1197         aBlock value: iterator next.
       
  1198     ].
       
  1199 ! !
       
  1200 
       
  1201 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1202 
       
  1203 do:aBlock separatedBy:betweenBlock
       
  1204     "evaluate the argument, aBlock for each element.
       
  1205      Between elements (i.e. after each except for the last),
       
  1206      evaluate betweenBlock.
       
  1207      This is a utility helper for collection printers
       
  1208      (for example, to print a space between elements)."
       
  1209 
       
  1210 "/ could do the more hackish:
       
  1211 "/
       
  1212 "/    |b|
       
  1213 "/
       
  1214 "/    b := [ b := betweenBlock ].
       
  1215 "/    self do:[:element |
       
  1216 "/        b value.
       
  1217 "/        aBlock value:element
       
  1218 "/    ].
       
  1219 "/
       
  1220 "/ but that creates a block, whereas the following does not.
       
  1221 
       
  1222     |first|
       
  1223 
       
  1224     first := true.
       
  1225     self do:[:element |
       
  1226         first ifTrue:[
       
  1227             first := false
       
  1228         ] ifFalse:[
       
  1229             betweenBlock value.
       
  1230         ].
       
  1231         aBlock value:element
       
  1232     ].
       
  1233 
       
  1234     "
       
  1235      #(1 2 3 4) do:[:el | Transcript show:el]
       
  1236                 separatedBy:[ Transcript show:'-']
       
  1237 
       
  1238      (Dictionary with:(1->'one') with:(2->'two'))
       
  1239          do:[:el | Transcript showCR:el printString]
       
  1240          separatedBy:[ Transcript showCR:'----']
       
  1241 
       
  1242      (Dictionary with:(1->'one') with:(2->'two'))
       
  1243         associations
       
  1244          do:[:el | Transcript showCR:el printString]
       
  1245          separatedBy:[ Transcript showCR:'----']
       
  1246 
       
  1247     "
       
  1248 
       
  1249     "Modified: / 11.2.2000 / 11:23:15 / cg"
       
  1250 ! !
       
  1251 
       
  1252 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1253 
       
  1254 inject:thisValue into:binaryBlock
       
  1255     "starting with thisValue for value, pass this value and each element
       
  1256      to binaryBlock, replacing value with the result returned from the block
       
  1257      in the next iteration.
       
  1258 
       
  1259      See also: #fold: #reduce:"
       
  1260 
       
  1261     |nextValue|
       
  1262 
       
  1263     nextValue := thisValue.
       
  1264     self do: [:each | nextValue := binaryBlock value:nextValue value:each].
       
  1265     ^ nextValue
       
  1266 
       
  1267     "sum up the elements of a collection:
       
  1268 
       
  1269      #(1 2 3 4) inject:0 into:[:accu :element | accu + element]   
       
  1270      (1 to:10) inject:0 into:[:accu :element | accu + element]     
       
  1271 
       
  1272      find the minimum:
       
  1273 
       
  1274      |coll|
       
  1275      coll := #(1 99 -15 20 100).
       
  1276      coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element]
       
  1277     "
       
  1278 
       
  1279     "Modified: 23.4.1996 / 13:47:06 / cg"
       
  1280 ! !
       
  1281 
       
  1282 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1283 
       
  1284 reject:aBlock
       
  1285     "return a new collection with all elements from the receiver, for which
       
  1286      the argument aBlock evaluates to false"
       
  1287 
       
  1288     ^ self select:[:element | (aBlock value:element) not]
       
  1289 
       
  1290     "
       
  1291      #(1 2 3 4) reject:[:e | e odd]   
       
  1292      (1 to:10) reject:[:e | e even]     
       
  1293     "
       
  1294 ! !
       
  1295 
       
  1296 !(Java classForName:'java.util.Collection') methodsFor:'* instance *'!
       
  1297 
       
  1298 select:aBlock
       
  1299     "return a new collection with all elements from the receiver, for which
       
  1300      the argument aBlock evaluates to true.
       
  1301      See also: #removeAllFoundIn: and #removeAllSuchThat:"
       
  1302 
       
  1303     |newCollection|
       
  1304 
       
  1305     newCollection := self species new.
       
  1306     self do:[:each |
       
  1307         (aBlock value:each) ifTrue:[newCollection add:each].
       
  1308     ].
       
  1309     ^ newCollection
       
  1310 
       
  1311     "
       
  1312      #(1 2 3 4) select:[:e | e odd]   
       
  1313      (1 to:10) select:[:e | e even]     
       
  1314     "
       
  1315 
       
  1316     "Modified: / 07-08-2010 / 16:26:40 / cg"
       
  1317 ! !
       
  1318 
       
  1319 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1320 
       
  1321 at: index
       
  1322     ^ self at: index ifAbsent:[ self subscriptBoundsError: index ]
       
  1323 ! !
       
  1324 
       
  1325 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1326 
       
  1327 at: index ifAbsent: errorBlock
       
  1328     (index between: 1 and: self size) ifTrue:[
       
  1329         ^ self get: index - 1
       
  1330     ].
       
  1331     ^ errorBlock value
       
  1332 ! !
       
  1333 
       
  1334 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1335 
       
  1336 at: index put: anObject
       
  1337     ^ self put: index - 1 value: anObject
       
  1338 ! !
       
  1339 
       
  1340 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1341 
       
  1342 fifth
       
  1343     ^ self get: 4
       
  1344 ! !
       
  1345 
       
  1346 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1347 
       
  1348 first
       
  1349     ^ self get: 0
       
  1350 ! !
       
  1351 
       
  1352 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1353 
       
  1354 fourth
       
  1355     ^ self get: 3
       
  1356 ! !
       
  1357 
       
  1358 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1359 
       
  1360 identityIndexOf:anElement
       
  1361     "Returns the index of an element or 0, if not found.
       
  1362      Compare using == (identity)"
       
  1363 
       
  1364     1 to: self size do:[:i | 
       
  1365         (self get: i - 1) == anElement ifTrue:[ ^ i ].
       
  1366     ].
       
  1367     ^ 0
       
  1368 ! !
       
  1369 
       
  1370 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1371 
       
  1372 indexOf:anElement
       
  1373     "Returns the index of an element or 0, if not found.
       
  1374      Compare using Object#equals()"
       
  1375 
       
  1376     ^ (self perform: #'indexOf(Ljava/lang/Object;)I' with: anElement) + 1. 
       
  1377 ! !
       
  1378 
       
  1379 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1380 
       
  1381 last
       
  1382     ^ self get: self size - 1
       
  1383 ! !
       
  1384 
       
  1385 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1386 
       
  1387 second
       
  1388     ^ self get: 1
       
  1389 ! !
       
  1390 
       
  1391 !(Java classForName:'java.util.List') methodsFor:'* instance *'!
       
  1392 
       
  1393 third
       
  1394     ^ self get: 2
       
  1395 ! !
       
  1396 
       
  1397 !(Java classForName:'java.util.Map') methodsFor:'* instance *'!
       
  1398 
       
  1399 at:aKey
       
  1400     "return the element indexed by aKey - report an error if none found"
       
  1401 
       
  1402     ^ self at:aKey ifAbsent:[self errorKeyNotFound:aKey]
       
  1403 ! !
       
  1404 
       
  1405 !(Java classForName:'java.util.Map') methodsFor:'* instance *'!
       
  1406 
       
  1407 at:key ifAbsent:exceptionBlock
       
  1408     "return the element indexed by aKey -
       
  1409      return result of exceptionBlock if no element is stored under aKey"      
       
  1410 
       
  1411     (self containsKey: key) ifTrue:[ ^ self get: key ].
       
  1412     ^ exceptionBlock value
       
  1413 ! !
       
  1414 
       
  1415 !(Java classForName:'java.util.Map') methodsFor:'* instance *'!
       
  1416 
       
  1417 do:aBlock
       
  1418     "perform the block for all values in the collection.
       
  1419 
       
  1420      See also:
       
  1421         #associationsDo:   (which passes key-value associations)
       
  1422         #keysAndValuesDo:  (which passes keys & values separately)
       
  1423         #keysDo:           (which passes keys only)
       
  1424 
       
  1425      WARNING: do not add/remove elements while iterating over the receiver.
       
  1426               Iterate over a copy to do this." 
       
  1427 
       
  1428     self values do: aBlock 
       
  1429 ! !
       
  1430 
       
  1431 !(Java classForName:'java.util.Map') methodsFor:'* instance *'!
       
  1432 
       
  1433 keysAndValuesDo:aTwoArgBlock
       
  1434     "evaluate the argument, aBlock for every element in the collection,
       
  1435      passing both key and element as arguments.
       
  1436 
       
  1437      See also:
       
  1438         #associationsDo:       (which passes keys->value pairs)
       
  1439         #do:                   (which only passes values)
       
  1440         #keysDo:               (which only passes keys)
       
  1441 
       
  1442      This is much like #associationsDo:, but aBlock gets the
       
  1443      key and value as two separate arguments."
       
  1444 
       
  1445     self keySet do:[:each | 
       
  1446         aTwoArgBlock value: each value: (self get: each).
       
  1447     ].
       
  1448 ! !
       
  1449 
   954 !LargeInteger class methodsFor:'queries'!
  1450 !LargeInteger class methodsFor:'queries'!
   955 
  1451 
   956 isJavaPrimitiveType
  1452 isJavaPrimitiveType
   957 
  1453 
   958     ^true
  1454     ^true
  2040 ! !
  2536 ! !
  2041 
  2537 
  2042 !ZipArchive methodsFor:'queries - java support'!
  2538 !ZipArchive methodsFor:'queries - java support'!
  2043 
  2539 
  2044 isValidFile: path
  2540 isValidFile: path
  2045     "Return true, if the recevier contains given file. false otherwise.
  2541     "Return true, if the recevier contains given file. false otherwise."
  2046     "
  2542 
  2047     ^zipMembersByName includesKey: path
  2543     ^ zipMembersByName notNil and:[zipMembersByName includesKey:path]
  2048 
  2544 
  2049     "Created: / 20-05-2013 / 23:37:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2545     "Created: / 20-05-2013 / 23:37:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2050 
       
  2051 ! !
  2546 ! !
  2052 
  2547 
  2053 !ZipArchive methodsFor:'reading - java support'!
  2548 !ZipArchive methodsFor:'reading - java support'!
  2054 
  2549 
  2055 nextBytes: bytesToRead of: zmember startingAt: pos into: b startingAt: off
  2550 nextBytes: bytesToRead of: zmember startingAt: pos into: b startingAt: off