Introduction of similar Swift types, functions, and methods in Java.
-
Download swift-for-java as a .zip file.
-
Open folder in Finder.
-
In another Finder window, navigate to the source folder of the desired IntelliJ project.
-
Drag the folder Swift into the source folder.
Please enable assertions in your compiler settings using the flag -ea
.
To enable assertions, go to File > Preferences > Build, Execution, Deployment > Compiler and type -ea
in the field labeled "Shared build process VM options".
To use Swift for Java in a scratch file, type -ea --enable-preview
instead in the aforementioned field.
Please ensure that both your version of IntelliJ IDEA and version of Java are updated to the most recent versions. Also, please ensure that your project language level is set to the highest number (not including X - Experimental Features).
- Java version 11.0.1
- IntelliJ IDEA 2018.2.5 (Community Edition)
- Project language level 11 - Local variable syntax for lambda parameters
In IntelliJ IDEA, navigate to File > Project Structure > Project and select 11 - Local variable syntax for lambda parameters in the field under Project language level.
In the desired class, Foo
, add the following:
public class Foo extends Swift { ... }
If you would like to use the SwiftArray
class, add the following line to your imports:
import Swift.SwiftArray;
-
print()
- prints an empty line -
print(item:)
- printsitem
-
print(items:)
- prints the specifieditems
separated by spaces (items
is a variadic argument) -
print(collection:separator:)
- prints the items incollection
separated by aseparator
-
print.withSeparator(separator:items:)
- prints the specifieditems
separated by aseparator
(items
is a variadic argument) -
print.withTerminator(terminator:items:)
- prints the specifieditems
followed by aterminator
(items
is a variadic argument) -
printr(item:)
- printsitem
and returnsitem
-
fatalError()
- crashes program (exit code 10) -
fatalError(message:)
- crashes program withmessage
(exit code 10)
-
precondition(condition:)
- throws anIllegalArgumentException
ifcondition
evaluates to false -
precondition(condition:message:)
- throws anIllegalArgumentException
with amessage
ifcondition
evaluates to false
-
swiftScanner(sc:)
- exposesScanner
to scanner methods-
hasNextWithPrefix(prefix:)
- returns aboolean
value indicating the presence ofprefix
at the beginning of the scanner object's next input -
hasNextWithSuffix(suffix:)
- returns aboolean
value indicating the presence ofsuffix
at the end of the scanner object's next input -
hasNextContaining(value:)
- returns aboolean
value indicating the presence ofvalue
contained within the scanner object's next input -
hasNextPositive(numberType:)
- returns aboolean
value indicating a positive input of the specifiednumberType
for the scanner object -
hasNextNegative(numberType:)
- returns aboolean
value indicating a negative input of the specifiednumberType
for the scanner object -
hasNextNumber()
- returns aboolean
value indicating whether the scanner object's next input is a number of any type -
hasNext(s:)
- returns aboolean
value indicating whether the scanner object's next input is the specified string,s
(supports regex inputs)
-
-
Non-mutating
-
reversed(str:)
- returns a copy of the inputted stringstr
reversed -
reversed(list:)
- returns a copy of the inputtedlist
reversed
-
-
Mutating
-
reverse(str:)
- reversesstr
-
reverse(list:)
- reverseslists
-
Ditch Java's weird conversion syntax of (targetType) value
for Swift's beautiful and clear type conversions.
// Conversion from double to int
int integerN = Int(3.1415926d);
// Conversion from int to double
double doubleN = Double(2);
// Conversion from int to long
long longN = Long(592);
// Conversion from double to String
String string = String(12345.6789);
Types with support for Swift-styled conversion:
(int) value
→Int(value)
(double) value
→Double(value)
(float) value
→Float(value)
(long) value
→Long(value)
(short) value
→Short(value)
(byte) value
→Byte(value)
new String(value)
→String(value)
Swift Math contains all of the same methods included within java.util.Math
along with an array of additional functions to make your life easier.
-
lcm(x:y:)
- returns the lowest common multiplier ofx
andy
-
factors(of:)
- returns aSwiftArray
containing the factors of an integral value -
primeFactors(of:)
- returns aSwiftArray
containing the prime factors of an integral value -
primality(of:)
- returns aboolean
value indicating whether an integral value is prime. -
isPalindromic(n:)
- returns aboolean
value indicating whether a value is a palindrome -
concatenate(x:y:)
- returns the value ofx
andy
concatenated -
factorial(n:)
- returns the value ofn!
-
average(collection:)
- returns adouble
value indicating the average of acollection
with numeric elements -
average(numbers:)
- returns adouble
value indicating the average of a variadic list ofnumbers
-
median(collection:)
- returns adouble
value indicating the median of acollection
with numeric elements -
median(numbers:)
- returns adouble
value indicating the median of a variadic list ofnumbers
-
mode(collection:)
- returns the first mode of acollection
-
mode(elements:)
- returns the first mode of a variadic list ofelements
-
modes(collection:)
- returns aSwiftArray
containing the modes of acollection
-
modes(elements:)
- returns the aSwiftArray
containing the modes of a variadic list ofelements
-
sum(collection:)
- returns the sum of all values in acollection
containing numeric elements -
sum(numbers:)
- returns the sum of a variadic list ofnumbers
SwiftArray
is an array class that contains all of the functions found in Swift's Collection
protocol and many more. SwiftArray
is incredibly versatile and brings many of Java's high order functions buried under Stream
to the surface with an ease of use.
-
SwiftArray()
,new SwiftArray()
- an empty array -
new SwiftArray(iterableElements:)
- an array containing the contents any type that is iterable (implementsIterable
) includingArrayList
,Stack
,LinkedList
, etc.
-
SwiftArray.fromRange(lowerBound:upperBound:)
- an array containing all the numbers in an inclusive range bounded bylowerBound
andupperBound
-
SwiftArray.repeating(element:count:)
- an array containingelement
repeatedcount
times -
SwiftArray.repeatingRandom(lowerBound:upperBound:count:)
- an array containingcount
random numbers within an inclusive range bounded bylowerBound
andupperBound
-
SwiftArray.repeatingRandom(lowerBound:upperBound:decimalPlaces:count:)
- an array containingcount
randomdouble
values rounded todecimalPlaces
decimal places within an inclusive range bounded bylowerBound
andupperBound
-
SwiftArray.strideTo(n:from:by:)
- an array from a starting value to, but not including, an end value, stepping by the specified amount -
SwiftArray.strideThrough(n:from:by:)
- an array from a starting value toward, and possibly including, an end value, stepping by the specified amount
Note: the word "array" in this context refers to a new object of SwiftArray
.
-
print()
- prints the array -
printr()
- prints and returns the array -
print(separator:)
- prints the array with its elements separated by aseparator
-
printr(separator:)
- prints and returns the array with its elements separated by aseparator
-
dump()
- dumps the array
-
append(item:)
- adds a new element at the end of the array -
append(items:)
- adds the new elements in a variadic list to the end of the array -
append(contentsOf:)
- adds the elements of a sequence to the end of the array. -
insert(element:atIndex:)
- inserts a new element at the specified position -
insert(contentsOf:atIndex:)
- inserts the elements of a sequence into the array at the specified position
-
count()
- the number of elements in the array -
count(predicate:)
- returns a count of the elements in the array that satisfy a given predicate
-
hasIndex(idx:)
- returns boolean value indicating the validity of the given offset in the array -
indices()
- an array containing the integral indices of an array -
startIndex()
- the position of the first element in a nonempty array -
endIndex()
- the array’s “past the end” position—that is, the position one greater than the last valid offset -
swapAt(i:j:)
- exchanges the values at the specified indices of the array
-
removeElement(atIndex:)
- removes and returns the element at the specified position -
removeFirst()
- removes and returns the element at the specified position -
removeFirst(n:)
- removes the specified number of elements from the beginning of the array -
removeLast()
- removes and returns the last element of the array -
removeLast(n:)
- Removes the specified number of elements from the end of the array -
removeAllWhere(predicate:)
- removes all the elements that satisfy the given predicate.
-
dropFirst()
- returns an array containing all but the first element of the initial array -
dropFirst(n:)
- returns an array containing all but the given number of initial elements -
dropLast()
- returns an array containing all but the last element of the initial array -
dropLast(n:)
- returns an array containing all but the specified number of final elements -
drop(predicate:)
- returns an array by skipping elements whilepredicate
returnstrue
and returning the remaining elements
-
prefix(n:)
- returns an array, up to the specified maximum length, containing the initial elements of the array -
prefixThrough(offset:)
- returns an array from the start of the initial array through the specified position -
prefixUpTo(offset:)
- returns an array from the start of the initial array up to, but not including, the specified position -
prefixWhile(predicate:)
- returns an array containing the initial elements untilpredicate
returnsfalse
and skipping the remaining elements
-
suffix(n:)
- returns an array, up to the given maximum length, containing the final elements of the initial array -
suffixFrom(offset:)
- returns an array from the specified position to the end of the initial array -
suffixWhile(predicate:)
- returns an array containing the final elements untilpredicate
returnsfalse
and skipping the remaining elements
-
first()
- the first element of the array -
firstWhere(predicate:)
- returns the first element of the array that satisfies the given predicate -
firstIndexWhere(predicate:)
- returns the first offset in which an element of the array satisfies the given predicate -
last()
- the last element of the array -
lastWhere(predicate:)
- returns the last element of the array that satisfies the given predicate -
lastIndexWhere(predicate:)
- returns the offset of the last element in the array that matches the given predicate
-
randomElement()
- returns a random element of the array -
randomIndex()
- returns a random offset of the array -
shuffle()
- shuffles the array in place -
shuffled()
- returns the elements of the array, shuffled
-
sort()
- sorts the array in places -
sort(c:)
- sorts the array in place, using the given predicate as the comparison between elements -
sorted()
- returns the elements of the array, sorted -
sorted(comparator:)
- returns the elements of the array, sorted using the given predicate as the comparison between elements
-
max(comparator:)
- returns the maximum element in the array, using the given predicate as the comparison between elements -
min(comparator:)
- returns the minimum element in the array, using the given predicate as the comparison between elements
-
reverse()
- reverses the elements of the array in place -
reversed()
- returns the elements of the array, reversed
-
joined()
- returns a new string by concatenating the elements of the array -
joined(separator:)
- returns a new string by concatenating the elements of the array, adding the given separator between each element
-
map(transform:)
- returns an array containing the results of mapping the given transformation over the array’s elements -
filter(predicate:)
- returns a new array containing, in order, the elements of the array that satisfy the given predicate -
reduce(accumulator:)
- returns the result of combining the elements of the array using the given accumulator
-
contains(predicate:)
- returns a boolean value indicating whether the array contains an element that satisfies the given predicate -
allSatisfy(predicate:)
- returns a boolean value indicating whether every element of an array satisfies a given predicate -
noneSatisfy(predicate:)
- returns a boolean value indicating whether no elements of an array satisfies a given predicate
reserveCapacity(n:)
- reserves enough space to store the specified number of elementstoSlice(lowerBound:upperBound:)
- returns a new array containing the elements from the lowerBound to the upperBound (inclusive)
This IntelliJ color theme a near replica of Xcode's beautiful dusk color theme for Java.
-
Then, inside IntelliJ, navigate to File > IntelliJ Idea > Prefereces > Editor > Color Scheme > General.
-
From there, select the gear icon to the right of the "Scheme" field. Continue to select Import Scheme > IntelliJ IDEA color scheme (.icls) or settings (.jar).
-
Inside the popup window of Finder, navigate to the downloaded project folder and select the file Dusk.icls and click "open".
-
Now hit "Apply", then "OK" in the bottom right of the IntelliJ preferences pane.
-
Finally your IntelliJ color scheme should be reminiscent of the Xcode's beautiful Dusk color scheme. Enjoy!
This project is licensed under the MIT License - see the LICENSE file for details