Skip to content

Commit

Permalink
Change type of btype from string to proper enum
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Dec 5, 2024
1 parent 986f2f4 commit fa48756
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 22 deletions.
15 changes: 4 additions & 11 deletions src/main/java/com/redhat/red/build/koji/KojiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -943,7 +938,7 @@ ListArchivesResponse.class, sessionUrlBuilder( session ),
public void enrichArchiveTypeInfo( List<KojiArchiveInfo> archives, KojiSessionInfo session )
throws KojiClientException
{
Map<String, List<KojiArchiveInfo>> buildTypeMap = archives.stream().collect( Collectors.groupingBy( KojiArchiveInfo::getBuildType ) );
Map<Btype, List<KojiArchiveInfo>> buildTypeMap = archives.stream().collect( Collectors.groupingBy( KojiArchiveInfo::getBuildType ) );

final AtomicReference<KojiClientException> err = new AtomicReference<>();

Expand All @@ -953,7 +948,7 @@ public void enrichArchiveTypeInfo( List<KojiArchiveInfo> archives, KojiSessionIn
{
switch ( buildType )
{
case "maven":
case MAVEN:
List<KojiMavenArchiveInfo> mavenArchiveInfos =
multiCall( Constants.GET_MAVEN_ARCHIVE, archiveIds, KojiMavenArchiveInfo.class,
session );
Expand All @@ -962,7 +957,7 @@ public void enrichArchiveTypeInfo( List<KojiArchiveInfo> archives, KojiSessionIn
archiveInfos.get( i ).addMavenArchiveInfo( mavenArchiveInfos.get( i ) );
}
break;
case "image":
case IMAGE:
List<KojiImageArchiveInfo> imageArchiveInfos =
multiCall( Constants.GET_IMAGE_ARCHIVE, archiveIds, KojiImageArchiveInfo.class,
session );
Expand All @@ -971,7 +966,7 @@ public void enrichArchiveTypeInfo( List<KojiArchiveInfo> archives, KojiSessionIn
archiveInfos.get( i ).addImageArchiveInfo( imageArchiveInfos.get( i ) );
}
break;
case "win":
case WIN:
List<KojiWinArchiveInfo> winArchiveInfos =
multiCall( Constants.GET_WIN_ARCHIVE, archiveIds, KojiWinArchiveInfo.class,
session );
Expand All @@ -980,8 +975,6 @@ public void enrichArchiveTypeInfo( List<KojiArchiveInfo> archives, KojiSessionIn
archiveInfos.get( i ).addWinArchiveInfo( winArchiveInfos.get( i ) );
}
break;
default:
logger.warn( "Unknown archive build type: {}", buildType );
}
}
catch ( KojiClientException e )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (C) 2015 Red Hat, Inc. ([email protected])
*
* 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<Btype>
{
@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();
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/redhat/red/build/koji/model/xmlrpc/Btype.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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" )
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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() )
{
Expand Down

0 comments on commit fa48756

Please sign in to comment.