Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

public enum Alignment: String {
case horizontal
case vertical
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

public enum Vehicle {
case bicycle
case car(String, trailer: String?)
case motorbike(String, horsePower: Int64, helmets: Int32?)
indirect case transformer(front: Vehicle, back: Vehicle)
case boat(passengers: Int32?, length: Int16?)

public init?(name: String) {
switch name {
case "bicycle": self = .bicycle
case "car": self = .car("Unknown", trailer: nil)
case "motorbike": self = .motorbike("Unknown", horsePower: 0, helmets: nil)
case "boat": self = .boat(passengers: nil, length: nil)
default: return nil
}
}

public var name: String {
switch self {
case .bicycle: "bicycle"
case .car: "car"
case .motorbike: "motorbike"
case .transformer: "transformer"
case .boat: "boat"
}
}

public func isFasterThan(other: Vehicle) -> Bool {
switch (self, other) {
case (.bicycle, .bicycle), (.bicycle, .car), (.bicycle, .motorbike), (.bicycle, .transformer): false
case (.car, .bicycle): true
case (.car, .motorbike), (.car, .transformer), (.car, .car): false
case (.motorbike, .bicycle), (.motorbike, .car): true
case (.motorbike, .motorbike), (.motorbike, .transformer): false
case (.transformer, .bicycle), (.transformer, .car), (.transformer, .motorbike): true
case (.transformer, .transformer): false
default: false
}
}

public mutating func upgrade() {
switch self {
case .bicycle: self = .car("Unknown", trailer: nil)
case .car: self = .motorbike("Unknown", horsePower: 0, helmets: nil)
case .motorbike: self = .transformer(front: .car("BMW", trailer: nil), back: self)
case .transformer, .boat: break
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"javaPackage": "com.example.swift",
"mode": "jni",
"logLevel": ["debug"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.swift.swiftkit.core.ClosableSwiftArena;
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
import org.swift.swiftkit.core.SwiftArena;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" })
public class EnumBenchmark {

@State(Scope.Benchmark)
public static class BenchmarkState {
ClosableSwiftArena arena;
Vehicle vehicle;

@Setup(Level.Trial)
public void beforeAll() {
arena = SwiftArena.ofConfined();
vehicle = Vehicle.motorbike("Yamaha", 900, OptionalInt.empty(), arena);
}

@TearDown(Level.Trial)
public void afterAll() {
arena.close();
}
}

@Benchmark
public Vehicle.Motorbike getAssociatedValues(BenchmarkState state, Blackhole bh) {
Vehicle.Motorbike motorbike = state.vehicle.getAsMotorbike().orElseThrow();
bh.consume(motorbike.arg0());
bh.consume(motorbike.horsePower());
return motorbike;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
import org.swift.swiftkit.core.SwiftArena;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

public class AlignmentEnumTest {
@Test
void rawValue() {
try (var arena = SwiftArena.ofConfined()) {
Optional<Alignment> invalid = Alignment.init("invalid", arena);
assertFalse(invalid.isPresent());

Optional<Alignment> horizontal = Alignment.init("horizontal", arena);
assertTrue(horizontal.isPresent());
assertEquals("horizontal", horizontal.get().getRawValue());

Optional<Alignment> vertical = Alignment.init("vertical", arena);
assertTrue(vertical.isPresent());
assertEquals("vertical", vertical.get().getRawValue());
}
}
}
Loading
Loading