From fa48756da5ab96c336766ccb89e08013478af0c0 Mon Sep 17 00:00:00 2001 From: David Walluck Date: Thu, 5 Dec 2024 11:36:42 -0500 Subject: [PATCH] Change type of btype from string to proper enum --- .../com/redhat/red/build/koji/KojiClient.java | 15 ++----- .../model/converter/KojiBtypeConverter.java | 40 +++++++++++++++++++ .../red/build/koji/model/xmlrpc/Btype.java | 40 +++++++++++++++++++ .../koji/model/xmlrpc/KojiArchiveInfo.java | 36 +++++++++++++---- .../koji/model/xmlrpc/KojiChecksumType.java | 7 ++-- 5 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/redhat/red/build/koji/model/converter/KojiBtypeConverter.java create mode 100644 src/main/java/com/redhat/red/build/koji/model/xmlrpc/Btype.java diff --git a/src/main/java/com/redhat/red/build/koji/KojiClient.java b/src/main/java/com/redhat/red/build/koji/KojiClient.java index f38bb271..ebda1297 100644 --- a/src/main/java/com/redhat/red/build/koji/KojiClient.java +++ b/src/main/java/com/redhat/red/build/koji/KojiClient.java @@ -63,15 +63,10 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -943,7 +938,7 @@ ListArchivesResponse.class, sessionUrlBuilder( session ), public void enrichArchiveTypeInfo( List archives, KojiSessionInfo session ) throws KojiClientException { - Map> buildTypeMap = archives.stream().collect( Collectors.groupingBy( KojiArchiveInfo::getBuildType ) ); + Map> buildTypeMap = archives.stream().collect( Collectors.groupingBy( KojiArchiveInfo::getBuildType ) ); final AtomicReference err = new AtomicReference<>(); @@ -953,7 +948,7 @@ public void enrichArchiveTypeInfo( List archives, KojiSessionIn { switch ( buildType ) { - case "maven": + case MAVEN: List mavenArchiveInfos = multiCall( Constants.GET_MAVEN_ARCHIVE, archiveIds, KojiMavenArchiveInfo.class, session ); @@ -962,7 +957,7 @@ public void enrichArchiveTypeInfo( List archives, KojiSessionIn archiveInfos.get( i ).addMavenArchiveInfo( mavenArchiveInfos.get( i ) ); } break; - case "image": + case IMAGE: List imageArchiveInfos = multiCall( Constants.GET_IMAGE_ARCHIVE, archiveIds, KojiImageArchiveInfo.class, session ); @@ -971,7 +966,7 @@ public void enrichArchiveTypeInfo( List archives, KojiSessionIn archiveInfos.get( i ).addImageArchiveInfo( imageArchiveInfos.get( i ) ); } break; - case "win": + case WIN: List winArchiveInfos = multiCall( Constants.GET_WIN_ARCHIVE, archiveIds, KojiWinArchiveInfo.class, session ); @@ -980,8 +975,6 @@ public void enrichArchiveTypeInfo( List archives, KojiSessionIn archiveInfos.get( i ).addWinArchiveInfo( winArchiveInfos.get( i ) ); } break; - default: - logger.warn( "Unknown archive build type: {}", buildType ); } } catch ( KojiClientException e ) diff --git a/src/main/java/com/redhat/red/build/koji/model/converter/KojiBtypeConverter.java b/src/main/java/com/redhat/red/build/koji/model/converter/KojiBtypeConverter.java new file mode 100644 index 00000000..21a5fb0a --- /dev/null +++ b/src/main/java/com/redhat/red/build/koji/model/converter/KojiBtypeConverter.java @@ -0,0 +1,40 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. (jcasey@redhat.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.redhat.red.build.koji.model.converter; + +import com.redhat.red.build.koji.model.xmlrpc.Btype; +import org.commonjava.rwx.core.Converter; + +public class KojiBtypeConverter + implements Converter +{ + @Override + public Btype parse( Object object ) + { + return Btype.fromString( object.toString() ); + } + + @Override + public Object render( Btype value ) + { + if ( value == null ) + { + return null; + } + + return value.getValue(); + } +} diff --git a/src/main/java/com/redhat/red/build/koji/model/xmlrpc/Btype.java b/src/main/java/com/redhat/red/build/koji/model/xmlrpc/Btype.java new file mode 100644 index 00000000..78f15b00 --- /dev/null +++ b/src/main/java/com/redhat/red/build/koji/model/xmlrpc/Btype.java @@ -0,0 +1,40 @@ +package com.redhat.red.build.koji.model.xmlrpc; + +public enum Btype +{ + RPM( "rpm " ), + MAVEN( "maven" ), + WIN( "win" ), + IMAGE( "image" ); + + private final String value; + + Btype( String value ) + { + this.value = value; + } + + public String getValue() + { + return value; + } + + public static Btype fromString( String name ) + { + for ( Btype btype : values() ) + { + if ( btype.value.equals( name ) ) + { + return btype; + } + } + + throw new IllegalArgumentException( "Unknown build type: " + name ); + } + + @Override + public String toString() + { + return value; + } +} diff --git a/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiArchiveInfo.java b/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiArchiveInfo.java index 428d138f..d49d8818 100644 --- a/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiArchiveInfo.java +++ b/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiArchiveInfo.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; +import com.redhat.red.build.koji.model.converter.KojiBtypeConverter; import com.redhat.red.build.koji.model.converter.KojiChecksumTypeConverter; import com.redhat.red.build.koji.model.converter.StringListConverter; import com.redhat.red.build.koji.model.util.ExternalizableUtils; @@ -46,17 +47,18 @@ public class KojiArchiveInfo implements Externalizable { - private static final int VERSION = 1; + private static final int VERSION = 2; - private static final long serialVersionUID = 6877608047448106469L; + private static final long serialVersionUID = -2526545088406954594L; @DataKey( "id" ) @JsonProperty( "id" ) private Integer archiveId; + @Converter( KojiBtypeConverter.class ) @DataKey( "btype" ) @JsonProperty( "btype" ) - private String buildType; + private Btype buildType; @DataKey( "btype_id" ) @JsonProperty( "btype_id" ) @@ -145,12 +147,12 @@ public KojiArchiveInfo() } - public String getBuildType() + public Btype getBuildType() { return buildType; } - public void setBuildType( String buildType ) + public void setBuildType( Btype buildType ) { this.buildType = buildType; } @@ -500,7 +502,16 @@ public void writeExternal( ObjectOutput out ) { out.writeInt( VERSION ); out.writeObject( archiveId ); - ExternalizableUtils.writeUTF( out, buildType ); + + if ( VERSION == 2 ) + { + out.writeObject( buildType ); + } + else + { + ExternalizableUtils.writeUTF( out, buildType.getValue() ); + } + out.writeObject( buildTypeId ); ExternalizableUtils.writeUTF( out, groupId ); ExternalizableUtils.writeUTF( out, artifactId ); @@ -531,13 +542,22 @@ public void readExternal( ObjectInput in ) { int version = in.readInt(); - if ( version != 1 ) + if ( version <= 0 || version > 2 ) { throw new IOException( "Invalid version: " + version ); } this.archiveId = (Integer) in.readObject(); - this.buildType = ExternalizableUtils.readUTF( in ); + + if ( version == 2 ) + { + this.buildType = (Btype) in.readObject(); + } + else + { + this.buildType = Btype.fromString( ExternalizableUtils.readUTF( in ) ); + } + this.buildTypeId = (Integer) in.readObject(); this.groupId = ExternalizableUtils.readUTF( in ); this.artifactId = ExternalizableUtils.readUTF( in ); diff --git a/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiChecksumType.java b/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiChecksumType.java index 7e86a7a1..a5adb849 100644 --- a/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiChecksumType.java +++ b/src/main/java/com/redhat/red/build/koji/model/xmlrpc/KojiChecksumType.java @@ -21,8 +21,9 @@ public enum KojiChecksumType sha1 ( 1, "SHA-1" ), sha256 ( 2, "SHA-256" ); - private Integer value; - private String algorithm; + private final Integer value; + + private final String algorithm; private KojiChecksumType( int value, String algorithm ) { @@ -40,7 +41,7 @@ public String getAlgorithm() return algorithm; } - public static KojiChecksumType fromInteger( Integer value ) + public static KojiChecksumType fromInteger( int value ) { for ( KojiChecksumType checksum : values() ) {