gpt4 book ai didi

java - 从java转换后如何修复 typescript 类文件错误?

转载 作者:行者123 更新时间:2023-12-02 01:33:37 24 4
gpt4 key购买 nike

我有一个 Java 类,我想在 typescript 项目中使用该类。但我尝试转换它并寻求 http://www.jsweet.org/jsweet-live-sandbox/ 的帮助。还。我对 typescript 很陌生,我是一名java开发人员,目前正在学习前端语言。所以我面临着识别错误的问题。如果您帮助我修复我的 typescript 代码,这将非常有帮助。

这是我的 Java 类:

import bd.edu.seu.erp.model.exception.IncorrectSemesterError;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Semester {
private static final String NAMES[] = {"Spring", "Summer", "Fall"};
private static final int OFFSET = 2002;

private static Map<Integer, Semester> semesterMap = new HashMap<>();
private static Map<String, Semester> semesterNameMap = new HashMap<>();

private int semesterNumber;
private String label;

public Semester(int semesterNumber, String label) {
this.semesterNumber = semesterNumber;
this.label = label;
}

private Semester(int semesterNumber) {
if (semesterNumber < 1)
throw new IncorrectSemesterError("Invalid Semester", "Semester number cannot be less than 1");
this.semesterNumber = semesterNumber;
this.label = NAMES[semesterNumber % NAMES.length] + " " + (semesterNumber / 3 + OFFSET);
}

private Semester(String semesterName) {
this.label = semesterName;
String[] tokens = semesterName.split("\\ ");
if (tokens.length != 2)
throw new IncorrectSemesterError("Invalid Semester", "Semester label has incorrect number of tokens");
String name = tokens[0];
int year = Integer.parseInt(tokens[1]);
if (year < OFFSET)
throw new IncorrectSemesterError("Invalid Semester", "Year cannot be earlier than " + OFFSET);
int nameIndex = Arrays.asList(NAMES).indexOf(name);
if (nameIndex < 0 || nameIndex > NAMES.length)
throw new IncorrectSemesterError("Invalid Semester", "Name of the semester must be one of [Spring, Summer, Fall]");
this.semesterNumber = (year - OFFSET) * 3 + nameIndex;
}

public static Semester of(int semesterNumber) {
Semester semester = semesterMap.getOrDefault(semesterNumber, new Semester(semesterNumber));
semesterMap.putIfAbsent(semester.semesterNumber, semester);
semesterNameMap.putIfAbsent(semester.label, semester);
return semester;
}

public static Semester of(String semesterName) {
Semester semester = semesterNameMap.getOrDefault(semesterName, new Semester(semesterName));
semesterMap.putIfAbsent(semester.semesterNumber, semester);
semesterNameMap.putIfAbsent(semester.label, semester);
return semester;
}

/*
public static Semester of(Semester semesterObject) {
Semester semester = semesterNameMap.getOrDefault(semesterObject.semesterNumber, new Semester(semesterObject.semesterNumber));
semesterMap.putIfAbsent(semester.semesterNumber, semester);
semesterNameMap.putIfAbsent(semester.label, semester);
return semester;
}
*/
}

这是我尝试过的 typescript 文件:


export class Semester {

static NAMES: string[] = ['Spring', 'Summer', 'Fall'];
static OFFSET = 2002;

static semesterMap: Map<number, Semester> = new Map();
static semesterNameMap: Map<String, Semester> = new Map();

private semesterNumber: number;
private label: String;

public constructor(semesterNumber?: number, label?: String) {
if (((typeof semesterNumber === 'number') || semesterNumber === null) && ((typeof label === 'string') || label === null)) {
if (this.semesterNumber === undefined) {
this.semesterNumber = 0;
}
if (this.label === undefined) {
this.label = null;
}
if (this.semesterNumber === undefined) {
this.semesterNumber = 0;
}
if (this.label === undefined) {
this.label = null;
}
(() => {
this.semesterNumber = semesterNumber;
this.label = label;
})();
} else if (((typeof label === 'string') || semesterNumber === null) && semesterNumber === undefined) {
if (this.semesterNumber === undefined) {
this.semesterNumber = 0;
}
if (this.label === undefined) {
this.label = null;
}
if (this.semesterNumber === undefined) {
this.semesterNumber = 0;
}
if (this.label === undefined) {
this.label = null;
}
(() => {
const tokens: string[] = label.split('\\ ');
if (tokens.length !== 2) {
throw new Error('Semester label has incorrect number of tokens');
}
const name: string = tokens[0];
const year: number = parseInt(tokens[1], 10);
if (year < Semester.OFFSET) {
throw new Error('Year cannot be earlier than ' + Semester.OFFSET);
}
const nameIndex: number = Semester.NAMES.slice(0).indexOf(name);
if (nameIndex < 0 || nameIndex > Semester.NAMES.length) {
throw new Error('Name of the semester must be one of [Spring, Summer, Fall]');
}
this.semesterNumber = (year - Semester.OFFSET) * 3 + nameIndex;
})();
} else if (((typeof semesterNumber === 'number'))) {
if (this.semesterNumber === undefined) {
this.semesterNumber = 0;
}
if (this.label === undefined) {
this.label = null;
}
if (this.semesterNumber === undefined) {
this.semesterNumber = 0;
}
if (this.label === undefined) {
this.label = null;
}
(() => {
if (semesterNumber < 1) {
throw new Error('Semester number cannot be less than 1');
}
this.semesterNumber = semesterNumber;
this.label = Semester.NAMES[semesterNumber % Semester.NAMES.length] + ' ' + (semesterNumber / 3 + Semester.OFFSET);
})();
} else {
throw new Error('invalid overload');
}
}

public static of(semesterNumber?: number, semesterName?: string): Semester {
if (typeof semesterNumber === 'number') {
const semester: Semester = Semester.semesterMap.getOrDefault(semesterNumber, new Semester(semesterNumber));
Semester.semesterMap.putIfAbsent(semester.semesterNumber, semester);
Semester.semesterNameMap.putIfAbsent(semester.label, semester);
return semester;
}
if (typeof semesterName === 'string') {
const semester: Semester = Semester.semesterNameMap.getOrDefault(semesterName, new Semester(semesterName));
Semester.semesterMap.putIfAbsent(semester.semesterNumber, semester);
Semester.semesterNameMap.putIfAbsent(semester.label, semester);
return semester;
}
}
}

首先,我对 typescript 构造函数感到困惑。我不确定,这是否是我为 java 中的多个构造函数编写的正确方法。其次,在 typescript 中我没有在Map中找到getOrDefault、putIfAbsent。我不明白这里应该写什么。

你能帮我修复这个 typescript 代码吗?提前致谢。

最佳答案

我也有 Java 背景,但年龄更大。 ;-)

一些建议:

  • 选择 undefinednull,但不应同时使用两者。在下面的示例中,我使用 undefined;
  • 在模块中,静态成员可以替换为简单变量;
  • 优先选择界面而不是多个可选参数;
  • 你的 IIFE ((() => { … }();) 毫无用处;
  • 当推理足够时,不要声明类型;
  • 更喜欢原始类型(string 而不是 String)。

这是一个例子:

const NAMES = ['Spring', 'Summer', 'Fall'];
const OFFSET = 2002;

const numberMap = new Map<number, Semester>();
const labelMap = new Map<string, Semester>();

export interface SemesterOptions {
semesterNumber?: number
label?: string
}

export class Semester {
private semesterNumber: number;
private label: string;

constructor({ semesterNumber, label }: SemesterOptions) {
if (semesterNumber === undefined) {
if (label === undefined) {
throw new Error('invalid options');
}
const parsed = parseLabelToSemesterOptions(label);
semesterNumber = parsed.semesterNumber;
label = parsed.label;
} else if (label === undefined) {
if (semesterNumber < 1) {
throw new Error('Semester number cannot be less than 1');
}
label = NAMES[semesterNumber % NAMES.length] + ' ' + (semesterNumber / 3 + OFFSET);
}

this.semesterNumber = semesterNumber;
this.label = label;
}


static ofNumber(semesterNumber: number): Semester {
let semester = numberMap.get(semesterNumber);
if (!semester) {
semester = new Semester({ semesterNumber });
numberMap.set(semester.semesterNumber, semester);
if (!labelMap.has(semester.label)) {
labelMap.set(semester.label, semester);
}
}
return semester;
}

static ofLabel(label: string): Semester {
let semester = labelMap.get(label);
if (!semester) {
semester = new Semester({ label });
labelMap.set(semester.label, semester);
if (!numberMap.has(semester.semesterNumber)) {
numberMap.set(semester.semesterNumber, semester);
}
}
return semester;
}
}

function parseLabelToSemesterOptions(labelToParse: string): Required<SemesterOptions> {
const tokens = labelToParse.split('\\ ');
if (tokens.length !== 2) {
throw new Error('Semester label has incorrect number of tokens');
}
const label = tokens[0];
const year = parseInt(tokens[1], 10);
if (year < OFFSET) {
throw new Error('Year cannot be earlier than ' + OFFSET);
}
const nameIndex = NAMES.indexOf(label);
if (nameIndex === -1) {
throw new Error(`Name of the semester must be one of ${NAMES.join(', ')}`);
}
const semesterNumber = (year - OFFSET) * 3 + nameIndex;
return {
semesterNumber,
label
}
}

对于getOrDefault:使用get并测试结果是否为 undefined (或 falsy )。有一个对 falsy 进行测试的快捷方式:

const val = myMap.get(myKey) || myDefautValue;

...但在您的情况下,if 语句更合适。

对于putIfAbsent:使用has首先测试 key 是否已经存在,然后使用 set如果需要的话。

关于java - 从java转换后如何修复 typescript 类文件错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55598377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com